KBD

Keith Devens .com

Friday, March 19, 2010 Flag waving
We ought always to deal justly, not only with those who are just to us, but likewise to those who... – Hierocles
← I want this hard driveStoring times in MySQL →

Daily link icon Thursday, July 15, 2004

'Show' function for debugging in PHP

I'm surprised I've never blogged this. Here's my "show" function I've used in PHP for a long time for debugging data structures. I have it right in my main file (dispatcher.php) so that it's always available.

<?php
function show($data$func "print_r"$return_str false){
    
ob_start();
    
$func($data);
    
$output '<pre>'.htmlspecialchars(ob_get_contents()).'</pre>';
    
ob_end_clean();
    if(
$return_str) return $output; else echo $output;
}
?>

This way, I don't have to do things like:

<?php
echo '<pre>';
var_dump($data);
echo 
'</pre>';
?>

all the time, and can just say show($data, 'var_dump') instead. Moreover, the code example I just gave doesn't escape the output of var_dump(), so if you may have stuff that needs to be escaped for HTML you'll essentially have to type out the contents of my show() function above to correctly display it Smiley winking.

The reason I'm blogging this now is because someone asked if he could see the source to my XML to PHP translator. I'm giving him an excerpt of it, and that excerpt contains show() (that's what actually prints everything if you run the translator). Here's a piece of that code:

<h2 id="xml">The original XML:</h2>
<?php show($f);?>
<h2 id="php">Resulting PHP data structure:</h2>
<?php show(XML_unserialize($f),$form->get('style'));?>

And, of course, that $form->get('style') bit is using my PHP form-processing library, Formation.

← I want this hard driveStoring times in MySQL →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.172s.

(Used 8 db queries)