is this possible in PHP? such as debug_print($s); debug_print($iInterestRate); debug_print($date->now()); print out: $s is Hello World $iInterestRate is 0.032 $date->now() is 1250403294320 it is possible in C and in Ruby... wonder if something like that is possible in PHP for easy debugging. Thanks very much!
Hi winterheat. I think I just responded to your question in another forum so I've copied the post for the benefit of anyone here that may want to know. -------------------------------------------------------------------------- I'm not familiar with any predefined method that does this in PHP. A simple way would simply be to print manually such as: print '$foo is '+$foo; PHP: However, I'm assuming that as you're asking the question, you have a lot of variables to debug and therefore want something a bit easier. I found this code on another forum but I haven't had a chance to test it myself. //============ Function : print_debug( arr result of get_defined_vars, mixed variable to debug) function print_debug($defined_vars_array, $debug){ $unset_unwanted = array("GLOBALS","_POST","_GET","_COOKIE","_FILES"); foreach ($unset_unwanted as $remove){ unset($defined_vars_array[$remove]); } while ($element = current($defined_vars_array)) { if ($element == $debug) { if (is_array($debug)){ echo '$'.key($defined_vars_array).' = <br />'. print_r($debug, TRUE) . "<br />"; } else { echo '$'.key($defined_vars_array).' = '. $debug . "<br />"; } } next($defined_vars_array); } } //============= End of Function //========================================= // somescript.php // Test Variable $test_var1 = "This is a test Variable"; $var_test = array(1,2,3); // debug the variable by calling "print_debug()" $arr = get_defined_vars(); print_debug($arr,$var_test); PHP: Source of code: http://groups.google.com/group/comp.lang.php/browse_thread/thread/e02037a5c14e54c9/3288eeef436b64f7 Let me know if this works.
Try this: <?php $test= ' something '; echo ' $test is '. $test; ?> The difference is in ' and " sign Same code, change the sign and see the output <?php $test= ' something '; echo " $test is ". $test; ?> First one will print the variable name, second will print it's value.
ah, I am actually looking *not* to use echo '$s is ', $s, "\n"; but just p($s) and it will accomplish the task of printing out $s is Hello World it is possible in C and in Ruby... wonder if it is possible in PHP.
is it true that... what if $s is Hello World and $b is Hello World, then will it always print the right variable name? (or if $a is 0 and $i is 0)