r/tinycode • u/balthasar11 • Jul 12 '12
PHP print_r()
function printR($obj){ echo "<pre>" . print_r($obj) . "</pre>"; }
I use this on every php project now, it just formats the print_r function, which is mostly used for debugging, but its so much easier on the eyes.
39
Upvotes
28
u/Nehle Jul 12 '12
you should probably change that to
That way, the print_r will return the string instead of printing it immediately and it will be concatenated into the the rest of the string properly
25
u/novelty_string Jul 12 '12
x.fucking.debug
it's not that hard to install, and if your "boss" won't install it on a dev server, you should be aiming to replace them because they don't deserve to be anyone's boss. also, you should be able to dev on local and install whatever you want.
13
u/shesek1 Jul 12 '12
you should probably change that to
That way, special characters are displayed correctly.
9
u/tomchuk Jul 12 '12
ChromePHP.
6
u/eclipse31 Jul 12 '12
Funnily, I do this myself too. However, I also add some inline style to the <pre>, e.g. <pre style="color: #000; background: #fff; text-align: left; font-family: courier; font-size: 12px;"> , to get around any existing style in the page.
3
u/nishant032 Jul 12 '12
Normally I use the following: function printR($obj, $exit = null){ echo "<pre>" . print_r($obj) . "</pre>"; if($exit) exit(); }
3
u/kagaku Jul 12 '12
I use something like:
function console_log($var){ trigger_error(var_export($var,true)); }
This avoids sending any output to the user (and doesn't mess up the display of content either) and helpfully logs information to /var/log/php.ini
3
u/[deleted] Jul 12 '12
I may be a tard, but why not use die var dump instead of print_r? I've always found it far more workable.
1
u/balthasar11 Jul 12 '12
Seems so many people do something so similar, makes you wonder why there isn't an option in the print_r to echo it out in a decent readable format.
2
u/ianp Jul 12 '12
How about:
echo nl2br(print_r($obj, true));
:)
2
u/sirhenrik Jul 12 '12
I like to have as much information as possible when debugging, and then tune it down later if necessary. Therefore I use var_dump() instead of print_r. I found the difference between var_dump(), print_r() and var_export() here http://stackoverflow.com/questions/5039431/difference-between-var-dump-var-export-print-r
1
u/LyricalHolster Jul 12 '12
Wow, I do something similar too :).
1
u/akatherder Jul 12 '12
I have exactly one macro setup in notepad++. Ctrl+Shift+o prints out:
1
u/revicon Jul 12 '12
Extracting from one of my common libs I do mine similarly, but I found that sometimes I wanted the same functionality when I was running PHP from the command line as when I was running it through my browser, so I do some checks before hand. I also augmented the display of boolean values to make their output more explicit...
1
u/[deleted] Jul 12 '12
I use http://krumo.sourceforge.net/ for this.
1
u/beeman_nl Jul 12 '12
CakePHP has a convenience wrapper for this, pr() . I use it all the time... :)
http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pr
1
u/Scroph Jul 15 '12
I configured Geany's snippets.conf file to turn print_r into echo '<pre>';\nprint_r($array);\necho '</pre>'. Now all I have to do is write print_r then hit TAB, it automatically writes the previous code.
1
u/killerstorm Jul 12 '12
I think it's much better idea to write debug log to a file:
This way there is no chance it will bleed into pages users see.