问题描述
我经常使用echo
和print_r
,几乎从不使用print
.
I use echo
and print_r
much, and almost never use print
.
我觉得echo
是宏,而print_r
是var_dump
的别名.
I feel echo
is a macro, and print_r
is an alias of var_dump
.
但这不是解释差异的标准方法.
But that's not the standard way to explain the differences.
推荐答案
print
和echo
大致相同;它们都是显示字符串的语言构造.区别很细微:print
的返回值为1,因此可以在表达式中使用,而echo
的返回类型为void
; echo
可以采用多个参数,尽管这种用法很少见. echo
比print
快一点. (就我个人而言,我始终使用echo
,从不使用print
.)
print
and echo
are more or less the same; they are both language constructs that display strings. The differences are subtle: print
has a return value of 1 so it can be used in expressions whereas echo
has a void
return type; echo
can take multiple parameters, although such usage is rare; echo
is slightly faster than print
. (Personally, I always use echo
, never print
.)
var_dump
打印出变量的详细转储,包括变量的类型和任何子项的类型(如果是数组或对象). print_r
以更易于理解的形式打印变量:不使用字符串引号,省略类型信息,不提供数组大小等.
var_dump
prints out a detailed dump of a variable, including its type and the type of any sub-items (if it's an array or an object). print_r
prints a variable in a more human-readable form: strings are not quoted, type information is omitted, array sizes aren't given, etc.
var_dump
通常比print_r
有用.当您不完全知道变量中具有哪些值/类型时,它特别有用.考虑以下测试程序:
var_dump
is usually more useful than print_r
when debugging, in my experience. It's particularly useful when you don't know exactly what values/types you have in your variables. Consider this test program:
$values = array(0, 0.0, false, '');
var_dump($values);
print_r ($values);
使用print_r
,您无法分辨0
和0.0
或false
和''
之间的区别:
With print_r
you can't tell the difference between 0
and 0.0
, or false
and ''
:
array(4) {
[0]=>
int(0)
[1]=>
float(0)
[2]=>
bool(false)
[3]=>
string(0) ""
}
Array
(
[0] => 0
[1] => 0
[2] =>
[3] =>
)
这篇关于PHP中的echo,print和print_r有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!