benjam Posted April 14, 2006 Share Posted April 14, 2006 I have a simple (yet [i]extremely[/i] useful) little debug tool that outputs a var or line of asterisks whenever it is called and was wondering if there is a way to get the var name in the output as well.Here is the current function:[code]function call($var = 'NULLNULL'){ if ('NULLNULL' === $var) { echo '<div style="font-size:large;font-weight:bold;color:red;background:white;">*****</div>'; } else { echo '<pre style="font-size:large;color:black;background:white;">'; if (is_bool($var) || is_null($var)) { var_dump($var); } else { print_r($var); } echo '</pre>'; }}[/code]What I would like it to do, is when I call it with the following:$my_var = 'This is My Variable';call($my_var);this is what it would output now:This is My VariableI would like it to output somthing like:$my_var = This is My VariableIs this possible? How? Quote Link to comment Share on other sites More sharing options...
benjam Posted April 17, 2006 Author Share Posted April 17, 2006 If this is not possible, please let me know. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 17, 2006 Share Posted April 17, 2006 You can loop throught the $GLOBALS array looking for the value that was passed. If you have more than one variable defined with the same value, you will get the first variable name.Here's an example:[code]<?php$test1 = 'this is a test';$test2 = 'this is another test';$test3 = $test1;call($test1);call($test2);call($test3);function call($var='NULLNULL'){ if ($var != 'NULLNULL') foreach($GLOBALS as $k => $v) if ($v == $var) {echo $k . '=' . $var . '<br>'; return; }}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
benjam Posted April 20, 2006 Author Share Posted April 20, 2006 I actually thought of that, and when developing, I use the call() function quite a lot, and I would rather not run though the GLOBALS array everytime I make a call to call().I also thought of the fact that it might not return the right variable name. Which is also not the desired effect.But if this is the only way... I may just have to use it.I was hoping there would be another way that I had not thought of. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 20, 2006 Share Posted April 20, 2006 Take a look at the [a href=\"http://www.php.net/array_search\" target=\"_blank\"]array_search()[/a] and [a href=\"http://www.php.net/array_keys\" target=\"_blank\"]array_keys()[/a] functions. These may help you.Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.