print_r() is an extremely useful PHP function that displays the information about just about any type of variable in a way that’s readable by humans. It is particularly essential for the display of arrays, making it often directed towards uncovering those all important system arrays of $_GET, $_POST, $_SERVER and of course, $_SESSION.
However the function by default prints out the information, sometimes not always the most useful of behaviours if you want to return its result as part of a function. However, not all is lost if you know about its second optional parameter, called $return.
$return is defaulted to false, meaning the function follows its default behaviour of printing out the information. However, if you set it to true, print_r() will in fact return what it would have printed as a string, perfect for stuffing into a variable and using elsewhere.
In practice:
$string = print_r($_GET,true); echo $string;
Nifty.
Related Link: http://php.net/manual/en/function.print-r.php
You might also enjoy:
-
Getting all the possible string permutations of an array can be pretty useful if you are trying to unlock something and this nifty little function does exac ...
-
The following function is useful if you wish to clear out all files and folders currently sitting in one particular directory. The function itself requires ...
-
Just a simple one this time around to give you the syntax for adding optional input variables to your PHP functions. This is pretty useful if you are going ...
-
Andrew Walker crafted this handy little PHP function which can convert a UTF-16 encoded string into a more PHP-friendly UTF-8 encoded string. The functio ...
-
I've written on the excellent jQuery Color Picker plugin from eyecon.ro before, but a question was recently raised on how to use its RGB output in code. Wel ...