KBD

Keith Devens .com

Friday, October 10, 2008 Flag waving
Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will... – Eric S. Raymond
← 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)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



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

Recent comments XML

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.206s.

(Used 8 db queries)

mobile phone