本文介绍了PHP中的echo,print和print_r有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用echoprint_r,几乎从不使用print.

I use echo and print_r much, and almost never use print.

我觉得echo是宏,而print_rvar_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.

推荐答案

printecho大致相同;它们都是显示字符串的语言构造.区别很细微:print的返回值为1,因此可以在表达式中使用,而echo的返回类型为voidecho可以采用多个参数,尽管这种用法很少见. echoprint 快一点. (就我个人而言,我始终使用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,您无法分辨00.0false''之间的区别:

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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:55