我经常使用echo和print_r,几乎从不使用print。
我觉得echo是一个宏,而print_r是var_dump的别名。
但这不是解释差异的标准方式。
我经常使用echo和print_r,几乎从不使用print。
我觉得echo是一个宏,而print_r是var_dump的别名。
但这不是解释差异的标准方式。
当前回答
回音:
它是语句而不是函数 没有返回值
不需要括号
非打印数组
打印
它是实函数
返回类型1
需要括号
非打印数组
Print_r
以人类可读的格式打印
字符串不加引号
不是变量的详细信息,如类型和所有
var_dump
所有变量的转储信息 比如元素和子元素的类型
其他回答
print_r()可以打印出值,但如果第二个标志参数被传递并且为TRUE -它将返回打印的结果作为字符串,而不发送到标准输出。 var_dump。如果安装了XDebug调试器,那么输出结果将以更易于阅读和理解的方式格式化。
echo
输出一个或多个以逗号分隔的字符串 没有返回值 例如,返回“字符串1”,“字符串2”
打印
只输出一个字符串 返回1,因此可以在表达式中使用 例如,打印“Hello” 或者,if ($expr && print "foo")
print_r ()
输出任何一个值的人类可读表示 不仅接受字符串,还接受包括数组和对象在内的其他类型,并将它们格式化为可读 调试时有用 如果给出第二个可选参数,可以将其输出作为返回值返回(而不是回显)
var_dump()
输出一个或多个用逗号分隔的值的人类可读表示 不仅接受字符串,还接受包括数组和对象在内的其他类型,并将它们格式化为可读 使用不同的输出格式print_r(),例如它也打印值的类型 调试时有用 没有返回值
var_export()
输出任意一个值的人类可读和php可执行表示 不仅接受字符串,还接受包括数组和对象在内的其他类型,并将它们格式化为可读 对print_r()和var_dump()使用不同的输出格式-产生的输出是有效的PHP代码! 调试时有用 如果给出第二个可选参数,可以将其输出作为返回值返回(而不是回显)
注:
Even though print can be used in an expression, I recommend people avoid doing so, because it is bad for code readability (and because it's unlikely to ever be useful). The precedence rules when it interacts with other operators can also be confusing. Because of this, I personally don't ever have a reason to use it over echo. Whereas echo and print are language constructs, print_r() and var_dump()/var_export() are regular functions. You don't need parentheses to enclose the arguments to echo or print (and if you do use them, they'll be treated as they would in an expression). While var_export() returns valid PHP code allowing values to be read back later, relying on this for production code may make it easier to introduce security vulnerabilities due to the need to use eval(). It would be better to use a format like JSON instead to store and read back values. The speed will be comparable.
它们都是语言结构。Echo返回void, print返回1。Echo被认为比print稍快。
echo
没有返回类型
print
有返回类型
print_r()
格式化的输出,
**回声可以接受多个表达式,而打印不能。 Print_r () PHP函数用于以人类可读的形式返回数组。它被简单地写成
![Print_r ($your_array)][1]