note 77221 added to function.var-dump

From: Date: Mon, 20 Aug 2007 18:54:33 +0000
Subject: note 77221 added to function.var-dump
Groups: php.notes 
Request: Send a blank email to php-notes+get-129648@lists.php.net to get a copy of this message
I made a function that I use instead of var_dump. It works well for hierarchical data. The trick is to used nested tables. May not be the best code but it's not that long so you could improve/modify it. Don't think it will work if any self-referencing is involved. <?php function variable_to_html($variable) { if ($variable === true) { return 'true'; } else if ($variable === false) { return 'false'; } else if ($variable === null) { return 'null'; } else if (is_array($variable)) { $html = "<table border=\"1\">\n"; $html .= "<thead><tr><td><b>KEY</b></td><td><b>VALUE</b></td></tr></thead>\n"; $html .= "<tbody>\n"; foreach ($variable as $key => $value) { $value = variable_to_html($value); $html .= "<tr><td>$key</td><td>$value</td></tr>\n"; } $html .= "</tbody>\n"; $html .= "</table>"; return $html; } else { return strval($variable); } } ?> Try it with this completely made-up data: <?php $meeting = array( 'title' => 'Sales Meeting', 'start_time' => array( 'hours' => 11, 'minutes' => 15, 'ampm' => 'am' ), 'end_time' => array( 'hours' => 1, 'minutes' => 30, 'ampm' => 'pm' ), 'attendees' => array( array('first_name' => 'Bob', 'last_name' => 'Smith', 'email' => 'bobsmith@email.com'), array('first_name' => 'James', 'last_name' => 'Andrews', 'email' => 'jamesandrews@email.com'), array('first_name' => 'Tom', 'last_name' => 'Schmoe', 'email' => 'bobsmith@email.com') ) ); echo variable_to_html($meeting); ?> ---- Server IP: 208.69.120.35 Probable Submitter: 71.245.168.151 ---- Manual Page -- http://www.php.net/manual/en/function.var-dump.php Edit -- https://master.php.net/note/edit/77221 Del: integrated -- https://master.php.net/note/delete/77221/integrated Del: useless -- https://master.php.net/note/delete/77221/useless Del: bad code -- https://master.php.net/note/delete/77221/bad+code Del: spam -- https://master.php.net/note/delete/77221/spam Del: non-english -- https://master.php.net/note/delete/77221/non-english Del: in docs -- https://master.php.net/note/delete/77221/in+docs Del: other reasons-- https://master.php.net/note/delete/77221 Reject -- https://master.php.net/note/reject/77221 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129648) next »