Home > php教程 > php手册 > PHP调试时print

PHP调试时print

WBOY
Release: 2016-06-06 19:58:26
Original
1076 people have browsed it

说到输出,不得不提到php中的打印了。 首先当然是最常用的echo了,echo :输出一个或多个字符串; print:和echo一样,但速度比echo慢。 print_r: 打印关于变量的易于理解的信息,如果给出的是 string、integer 或 float,将打印变量本身。如果给出的是 array

说到输出,不得不提到php中的打印了。

首先当然是最常用的echo了,echo :输出一个或多个字符串;

print:和echo一样,但速度比echo慢。
print_r:
           打印关于变量的易于理解的信息,如果给出的是 string、integer 或 float,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。 记住,print_r() 将把数组的指针移到最后边。使用 reset() 可让指针回到开始处。

var_export:跟print_r和var_dump类似,但是很少用。
var_dump:
           此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。
var_dump和print_r的区别:
            var_dump返回表达式的类型与值而print_r仅返回结果,相比调试代码使用var_dump更便于阅读。

print_r(),var_export(),var_dump()的区别具体见下:

eg:二维数组的输出:

$arr=array(array('a'=>'aa','b'=>'bbb','c'=>'ccc'),
           array('a'=>'ddd','b'=>'eee','c'=>'fff'),
           array('a'=>'gg','b'=>'hh'));
print_r($arr);
//print_r输出:
 //Array ( [0] => Array ( [a] => aa [b] => bbb [c] => ccc )
                   [1] => Array ( [a] => ddd [b] => eee [c] => fff )
                   [2] => Array ( [a] => gg [b] => hh ) )
var_export($arr);
//var_export输出:
 //array ( 0 => array ( 'a' => 'aa', 'b' => 'bbb', 'c' => 'ccc', ),
                   1 => array ( 'a' => 'ddd', 'b' => 'eee', 'c' => 'fff', ),
                   2 => array ( 'a' => 'gg', 'b' => 'hh', ), )
var_dump($arr);
//var_dump输出:
/* array (size=3)
  0 =>
  array (size=3)
   'a' => string 'aa' (length=2)
   'b' => string 'bbb' (length=3)
   'c' => string 'ccc' (length=3)
  1 =>
  array (size=3)
           'a' => string 'ddd' (length=3)
   'b' => string 'eee' (length=3)
   'c' => string 'fff' (length=3)
  2 =>
  array (size=2)
   'a' => string 'gg' (length=2)
   'b' => string 'hh' (length=2)
*/

下面在给出一个json格式的输出:
$arr=array(array('a'=>'aa','b'=>'bbb','c'=>'ccc'),
           array('a'=>'ddd','b'=>'eee','c'=>'fff'),
           array('a'=>'gg','b'=>'hh'));
$arra=json_encode($arr);
print_r($arra);
//print_r输出:
 [{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]
var_export($arra);
//var_export输出:
 '[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]'
var_dump($arra);
//var_dump输出:
 string '[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]' (length=84) 

 

 具体的区别,我想大家一看就可以明白了吧

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template