|
|
 |
print_r (PHP 4 , PHP 5) print_r --
打印关于变量的易于理解的信息。
描述bool print_r ( mixed expression [, bool return]) 注:
参数 return 是在 PHP 4.3.0 的时候加上的
print_r() 显示关于一个变量的易于理解的信息。如果给出的是
string、integer
或 float,将打印变量值本身。如果给出的是
array,将会按照一定格式显示键和元素。object
与数组类似。
记住,print_r() 将把数组的指针移到最后边。使用
reset() 可让指针回到开始处。
上边的代码将输出:
<pre>
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</pre> |
如果想捕捉 print_r() 的输出,可使用
return 参数。若此参数设为
TRUE,print_r() 将不打印结果(此为默认动作),而是返回其输出。
例子 1. return 参数示例 |
<?php
$b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));
$results = print_r ($b, true); //$results 包含了 print_r 的输出结果
?>
|
|
注:
如果想在 PHP 4.3.0 之前的版本中捕捉
print_r() 的输出,可使用输出控制函数。
注:
在 PHP 4.0.4 之前的版本中,如果给出的
array 或 object 包含了直接或间接指向自身的引用,print_r()
将永远继续下去。print_r($GLOBALS)
就是一个例子,因为 $GLOBALS
自身即是全局变量,其包含了指向自身的引用。
参见 ob_start()、var_dump()
和 var_export()。
hlecuanda at autologue dot com_NOSPAM
07-Jul-2001 07:24
I've written this little function using print_r. it works very nicely.
Just be sure to have a global variable called $debug. if you set it to
true, dp() will activate, if set to false, nothing will happen. Nice when
developing/debugging code with lots of arrays and objects =)!
It
will print a beautified version of your call.
function
dp($call,$cname) {
// call: the variable you want to print_r
//
cname: the label for your debugging output
global $debug;
if ($debug) {
echo $cname.":<pre>";
if (!is_array($call)) { $call=htmlspecialchars($call); }
print_r($call);
if ( is_array($call)) { reset($call); }
echo "</pre><hr>";
} // end function
dp() ======================
example:
$debug=true;
dp($some_array_or_object,"Label for
object");
| |