This is just a quick tip, but ever since discovering it I use it constantly. A lot of developers use PHP’s print_r()
function to display human-readable information about a variable, which makes it great for debugging arrays and objects (among other things) very quickly. However, you have to wrap the output in <pre></pre>
tags otherwise it gets spit out in a jumbled mess and doesn’t display nicely.
echo‘‘ ;print_r($foo);echo‘‘;
|
That works just fine, but you can shorten it up by passing true to
print_r()
‘s second parameter, the
$return
flag, and mixing it in with your
echo
statements. What this does is tells
print_r()
to return the human-readable variable data instead of printing it to the screen right away.
echo‘‘ .print_r($foo,true).‘‘;
|
Earth-shattering code breakthrough? Absolutely (not). However, it does turn three lines of code into one and makes it quicker (for me, anyway) to write quick debugging statements and move them around easier.
As an added bonus, you can “queue up” a bunch of print_r()
‘s by capturing their output in a variable and then echo’ing it just once.
$debug=print_r($foo,true);$debug.=print_r($bar,true);echo‘‘ .$debug.‘‘;
|
Voila, shorter debugging statements that are easier to work with.
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn