note 106202 added to function.print-r
| From: | darkdevine at hotmail dot de | Date: | Tue, 18 Oct 2011 13:13:50 +0000 |
| Subject: | note 106202 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183957@lists.php.net to get a copy of this message | ||
None of the custom print_r functions here and in the var_dump notes worked for me.
The biggest problem regarding such functions is the ability to find references to prevent recursion.
Imagine the following code:
<?php
$root = new XmlRoot( 'myRoot' );
$node = new XmlNode( 'myNode' );
$root->AddChildren( $node );
// AddChildren will add $node to $root->_Children and will set $node->_Root to $root
?>
If we iterate $node or $root using a custom dump function that uses foreach() to jump over the
values, the properties would permanently reference themself and print again and again, which is
called recursion
Now here's my solution for this:
http://pastebin.com/B8JRA5XE
This function prints the passed argument in a very clean and nice format using colors and
white-spaces.
I prevent recursion like this:
Upon calling the function, the passed argument is serialized.
The MD5 hash of the serialized string is the key we use to detect, if the same object/array has
already passed.
We don't really save the values, we just save the hash (I prefer isset() over in_array,
that's why I'm only using the keys there)
Then the function will check, if the MD5 string already exists, if yes, the type that falls into the
switch() is changed to "recursion" and prints a string later on.
I use many static variables for the sake of usability, the function accepts multiple parameters like
var_dump does and prints every single one of them in a nice format (or rather, it gives the string
to print to you, which has more use than just echo'ing it imho)
Feel free to use it
----
Server IP: 212.124.37.9
Probable Submitter: 217.92.153.78
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/106202
Del: integrated -- https://master.php.net/note/delete/106202/integrated
Del: useless -- https://master.php.net/note/delete/106202/useless
Del: bad code -- https://master.php.net/note/delete/106202/bad+code
Del: spam -- https://master.php.net/note/delete/106202/spam
Del: non-english -- https://master.php.net/note/delete/106202/non-english
Del: in docs -- https://master.php.net/note/delete/106202/in+docs
Del: other reasons-- https://master.php.net/note/delete/106202
Reject -- https://master.php.net/note/reject/106202
Search -- https://master.php.net/manage/user-notes.php