超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<issetserialize>
Last updated: Fri, 22 Jun 2007

print_r

(PHP 4 , PHP 5)

print_r --  打印关于变量的易于理解的信息。

描述

bool print_r ( mixed expression [, bool return])

注: 参数 return 是在 PHP 4.3.0 的时候加上的

print_r() 显示关于一个变量的易于理解的信息。如果给出的是 stringintegerfloat,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。

记住,print_r() 将把数组的指针移到最后边。使用 reset() 可让指针回到开始处。

<pre>
<?php
   $a
= array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
  
print_r ($a);
?>
</pre>

上边的代码将输出:
<pre>
Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
</pre>

如果想捕捉 print_r() 的输出,可使用 return 参数。若此参数设为 TRUEprint_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 之前的版本中,如果给出的 arrayobject 包含了直接或间接指向自身的引用,print_r() 将永远继续下去。print_r($GLOBALS) 就是一个例子,因为 $GLOBALS 自身即是全局变量,其包含了指向自身的引用。

参见 ob_start()var_dump()var_export()




add a note add a note User Contributed Notes
print_r
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");

<issetserialize>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22