there have been a few suggestions in this thread regarding how to benefit from var_dump and print_r. Over the years I've come to the following dumping functions which are available in all of my PHP projects and are quite handy.
Simply spoken, "vd" means "var_dump" only that is shorter and more useful.
"vdd" means "var_dump and die" - stops execution immediatly after output.
* vd functions version 1.1 by matto
* dump any object
* set return_it to true so the dump will be returned as a string
* otherwise it will be dumped to the screen
* html_entities filter will be applied
* @param mixed $anobject, boolean return_it
function vd($anobject, $return_it=false)
$out = ob_get_contents();
Weedpacket mention Xdebug just above, which can also format the output of var_dump() for a browser. It also creates some process log files which can be analysed in KCacheGrind or the windows equivalent WinCacheGrind. Handy for finding code bottlenecks.
As the programmer, it is your responsibility to understand what your code is doing and what it is supposed to do; the computer is not capable of doing that for you. Taking chunks of other people's code and inserting them into yours without bothering to work out how to use them is a sure way of turning your code into an unworkable mess.
If it's a class, study its interface to see what it provides and then decide if you can use it to do what you're wanting. If it's just a fragment of code of the sort that is posted here to illustrate a solution, remember that it is just an illustration and not necessarily the real thing. Who knows what it will do when you run it? If you don't, you're in trouble.
Last edited by Weedpacket; 03-03-2007 at 08:19 PM.
Testing in a browser is handy for seeing HTML output, but a lot of other things are lost. If you run your script from the command line you can see whether or not PHP is "fixing" your code for you, particularly in the case of uninitiated variables that PHP sometimes handles on its own.
Last edited by banzaimonkey; 04-06-2007 at 09:26 PM.
Efficacy before Efficiency; Edit for Excellence Blog - Site
When using (uninitialized) variables that do not exist, errors of level E_NOTICE are produced and you cannot stop the PHP voodoo magic (like making $foo[bar] turn bar into 'bar'), it happens, but to go along with the magic you can produce (and find) the errors if (when) you turn them on.
People are encouraged to log all errors instead of using display_errors = on but regardless the main goal here (on this specific subtopic of eliminating php errors) is to turn on ALL errors, test the code, and eliminate said errors.