|
 |
print_r (PHP 4, PHP 5) print_r --
Prints human-readable information about a variable
Descriptionbool print_r ( mixed expression [, bool return] ) Замечание:
The return parameter was added in PHP 4.3.0
print_r() displays information about a variable
in a way that's readable by humans. If given a string,
integer or float, the value itself will be
printed. If given an array,
values will be presented in a format that shows keys and
elements. Similar notation is used for objects.
print_r() and var_export() will
also show protected and private properties of objects with PHP 5, on the
contrary to var_dump().
Remember that print_r() will move the array
pointer to the end. Use reset() to bring
it back to beginning.
If you would like to capture the output of print_r(),
use the return parameter. If this parameter is set
to TRUE, print_r() will return its output, instead of
printing it (which it does by default).
Пример 1. return parameter example
<?php
$b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));
$results = print_r($b, true); ?>
|
|
Замечание:
If you need to capture the output of print_r() with a
version of PHP prior to 4.3.0, use the
output-control functions.
Замечание:
Prior to PHP 4.0.4, print_r() will continue forever
if given an array or object that
contains a direct or indirect reference to itself. An example
is print_r($GLOBALS) because
$GLOBALS is itself a global variable that
contains a reference to itself.
See also ob_start(),
var_dump() and
var_export().
print_r
ohira (atto) web. de
13-Apr-2006 07:01
Bases on thbley´s sript i use this one to log some actions.
It will return a tabbed like string which you can output or whatever.
Input fields like "Password" will not be shown.
<?php
function print_r_string($arr,$first=true,$tab=0)
{
$output = "";
$tabsign = ($tab) ? str_repeat(' ',$tab) : '';
if ($first) $output .= "<pre><br>\n";
foreach($arr as $key => $val)
{
switch (gettype($val))
{
case "array":
$output .= $tabsign."[".htmlspecialchars($key)."] = array(".count($val).")<br>\n".$tabsign."(<br>\n";
$tab++;
$output .= print_r_string($val,false,$tab);
$tab--;
$output .= $tabsign.")<br>\n";
break;
case "boolean":
$output .= $tabsign."[".htmlspecialchars($key)."] bool = '".($val?"true":"false")."'<br>\n";
break;
case "integer":
$output .= $tabsign."[".htmlspecialchars($key)."] int = '".htmlspecialchars($val)."'<br>\n";
break;
case "double":
$output .= $tabsign."[".htmlspecialchars($key)."] double = '".htmlspecialchars($val)."'<br>\n";
break;
case "string":
$output .= $tabsign."[".htmlspecialchars($key)."] string = '".((stristr($key,'passw')) ? str_repeat('*', strlen($val)) : htmlspecialchars($val))."'<br>\n";
break;
default:
$output .= $tabsign."[".htmlspecialchars($key)."] unknown = '".htmlspecialchars(gettype($val))."'<br>\n";
break;
}
}
if ($first) $output .= "</pre><br>\n";
return $output;
}
echo print_r_string(array($_POST,$_GET)); ?>
sanya at hik dot hu
05-Apr-2006 07:13
I find a possibly bug, when I wanted to print the structure of a DOMDocument or a DOMDocumentType object.
print_r() outputs the object type correctly, but the properties are missing.
$doc = $imp->createDocument();
$doc->loadXML($xmlString);
print_r($doc->doctype);
the code outputs:
DOMDocumentType Object
(
)
But the subobjects of $doc->doctype exist.
reinder at fake-address dot com
31-Jan-2006 09:09
I always use this function in my code, because most of my functions return an Array or Boolean :
<?php
function printr ( $object , $name = '' ) {
print ( '\'' . $name . '\' : ' ) ;
if ( is_array ( $object ) ) {
print ( '<pre>' ) ;
print_r ( $object ) ;
print ( '</pre>' ) ;
} else {
var_dump ( $object ) ;
}
}
?>
( print_r gives no output on FALSE and that can be annoying! )
13-Dec-2005 01:00
function print_pre($var){
echo '<pre>';
print_r($var);
echo '</pre>'
}
thbley at gmail dot com
16-Nov-2005 12:10
Here is a print_r that produces xml:
(now you can expand/collapse the nodes in your browser)
<?php
header('Content-Type: text/xml; charset=UTF-8');
echo print_r_xml($some_var);
function print_r_xml($arr,$first=true) {
$output = "";
if ($first) $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<data>\n";
foreach($arr as $key => $val) {
if (is_numeric($key)) $key = "arr_".$key; switch (gettype($val)) {
case "array":
$output .= "<".htmlspecialchars($key)." type='array' size='".count($val)."'>".
print_r_xml($val,false)."</".htmlspecialchars($key).">\n"; break;
case "boolean":
$output .= "<".htmlspecialchars($key)." type='bool'>".($val?"true":"false").
"</".htmlspecialchars($key).">\n"; break;
case "integer":
$output .= "<".htmlspecialchars($key)." type='integer'>".
htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
case "double":
$output .= "<".htmlspecialchars($key)." type='double'>".
htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
case "string":
$output .= "<".htmlspecialchars($key)." type='string' size='".strlen($val)."'>".
htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
default:
$output .= "<".htmlspecialchars($key)." type='unknown'>".gettype($val).
"</".htmlspecialchars($key).">\n"; break;
}
}
if ($first) $output .= "</data>\n";
return $output;
}
?>
jamin42b at gmail dot com
02-Sep-2005 05:52
Here's a short function that can export php arrays to javascript arrays.
<?php
function php_to_js($array, $base) {
$js = '';
foreach ($array as $key=>$val) {
if (is_array($val)) {
$js .= php_to_js($val, $base.(is_numeric($key) ? '['.$key.']' : "['".addslashes($key)."']"));
} else {
$js .= $base;
$js .= is_numeric($key) ? '['.$key.']' : "['".addslashes($key)."']";
$js .= ' = ';
$js .= is_numeric($val) ? ''.$val.'' : "'".addslashes($val)."'";
$js .= ";\n";
}
}
return $base." = new Array();\n".$js;
}
?>
Example use:
<?php
$my_array = array('gdsag' => 4, 'hello', array(5, 6));
echo '<script>'.php_to_js($my_array).'</script>';
?>
This would output:
<script>
jsvarname = new Array();
jsvarname['gdsag'] = 4;
jsvarname[0] = 'hello';
jsvarname[1] = new Array();
jsvarname[1][0] = 5;
jsvarname[1][1] = 6;
</script>
Now the array is loaded in the browser as javascript. As you can see, it supports multidimensional arrays too.
warhog at warhog dot net
11-Aug-2005 05:01
For very long arrays I have written a little function which formats an array quite nice and uses javascript for browsing it like a tree. The function is very customizable with the $style parameter.
For me it's of great use for browsing large array's, for example when those are used in language-files in some script and so on. It may even be used in "real" scripts for the "real" front-end, cause the tree can very easily be styled (look at the function or the outputted source and you'll see what i mean).
Here's the function:
<?php
function print_r_html($arr, $style = "display: none; margin-left: 10px;")
{ static $i = 0; $i++;
echo "\n<div id=\"array_tree_$i\" class=\"array_tree\">\n";
foreach($arr as $key => $val)
{ switch (gettype($val))
{ case "array":
echo "<a onclick=\"document.getElementById('";
echo array_tree_element_$i."').style.display = ";
echo "document.getElementById('array_tree_element_$i";
echo "').style.display == 'block' ?";
echo "'none' : 'block';\"\n";
echo "name=\"array_tree_link_$i\" href=\"#array_tree_link_$i\">".htmlspecialchars($key)."</a><br />\n";
echo "<div class=\"array_tree_element_\" id=\"array_tree_element_$i\" style=\"$style\">";
echo print_r_html($val);
echo "</div>";
break;
case "integer":
echo "<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."</i><br />";
break;
case "double":
echo "<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."</i><br />";
break;
case "boolean":
echo "<b>".htmlspecialchars($key)."</b> => ";
if ($val)
{ echo "true"; }
else
{ echo "false"; }
echo "<br />\n";
break;
case "string":
echo "<b>".htmlspecialchars($key)."</b> => <code>".htmlspecialchars($val)."</code><br />";
break;
default:
echo "<b>".htmlspecialchars($key)."</b> => ".gettype($val)."<br />";
break; }
echo "\n"; }
echo "</div>\n"; }
?>
The function as it is now does not support the $return parameter as print_r does and will create an endless loop like print_r did in php-versions < 4.0.3 when there is an element which contains a reference to a variable inside of the array to print out :-/
I've tested it with PHP 5.0.6 and PHP 4.2.3 - no problems except those already mentioned.
please e-mail me if you've got a solution for the problems i've mentioned, i myself are not able to solve them 'cause i don't know how the hell i can find out whether a variable is a reference or not.
nicky dot weber at siner dot de
11-Jun-2005 02:13
Print arrays formatted for a browser easily:
<?php
function print_html_r( $aData ) {
echo nl2br( eregi_replace( " ", " ", print_r( $data, TRUE ) ) );
}
?>
general at NOSPAMbugfoo dot com
20-Jan-2005 01:23
You can't use print_r($var, TRUE) inside a function which is a callback from ob_start() or you get the following error:
Fatal error: print_r(): Cannot use output buffering in output buffering display handlers
| |