PHP中echo、print和print_r之间的区别是什么?

时间:2022-10-10 22:26:44

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

我使用echo和print_r,而且几乎从不使用print。

I feel echo is a macro, and print_r is an alias of var_dump.

我觉得echo是一个宏,print_r是var_dump的别名。

But that's not the standard way to explain the differences.

但这不是解释差异的标准方法。

10 个解决方案

#1


171  

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.)

打印和回波或多或少是相同的;它们都是显示字符串的语言结构。差异是细微的:print的返回值为1,因此可以在表达式中使用,而echo具有void返回类型;echo可以使用多个参数,虽然这种用法很少见;echo的速度略快于print。(就我个人而言,我总是使用echo,从不打印。)

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:

根据我的经验,在调试时,var_dump通常比print_r更有用。当您不知道您的变量中有哪些值/类型时,它特别有用。考虑一下这个测试程序:

$values = array(0, 0.0, false, '');

var_dump($values);
print_r ($values);

With print_r you can't tell the difference between 0 and 0.0, or false and '':

对于print_r,您不能区分0和0.0,或false和“:

array(4) {
  [0]=>
  int(0)
  [1]=>
  float(0)
  [2]=>
  bool(false)
  [3]=>
  string(0) ""
}

Array
(
    [0] => 0
    [1] => 0
    [2] => 
    [3] => 
)

#2


86  

echo

回声

  • Outputs one or more strings separated by commas
  • 输出一个或多个用逗号分隔的字符串。
  • No return value

    没有返回值

    e.g. echo "String 1", "String 2"

    例如,echo "String 1", "String 2"

print

打印

  • Outputs only a single string
  • 只输出一个字符串。
  • Returns 1, so it can be used in an expression

    返回1,因此可以在表达式中使用。

    e.g. print "Hello"

    如打印“你好”

    or, if ($expr && print "foo")

    或者,if ($expr && print "foo")

print_r()

print_r()

  • Outputs a human-readable representation of any one value
  • 输出一个人可读的任何一个值的表示。
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读。
  • Useful when debugging
  • 当调试有用
  • May return its output as a return value (instead of echoing) if the second optional argument is given
  • 如果给出第二个可选参数,可以将其输出返回为返回值(而不是echo) ?

var_dump()

var_dump()

  • Outputs a human-readable representation of one or more values separated by commas
  • 输出以逗号分隔的一个或多个值的可读表示。
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读。
  • Uses a different output format to print_r(), for example it also prints the type of values
  • 使用与print_r()不同的输出格式,例如,它也打印值的类型。
  • Useful when debugging
  • 当调试有用
  • No return value
  • 没有返回值

var_export()

var_export()

  • Outputs a human-readable and PHP-executable representation of any one value
  • 输出任何一个值的可读和可执行的表示。
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读。
  • Uses a different output format to both print_r() and var_dump() - resulting output is valid PHP code!
  • 对print_r()和var_dump()使用不同的输出格式,结果输出是有效的PHP代码!
  • Useful when debugging
  • 当调试有用
  • May return its output as a return value (instead of echoing) if the second optional argument is given
  • 如果给出第二个可选参数,可以将其输出返回为返回值(而不是echo) ?

Notes:

注:

  • 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.
  • 尽管可以在表达式中使用print,但我建议人们避免这样做,因为它不利于代码可读性(而且因为它不太可能有用)。当它与其他操作符交互时,优先规则也会令人迷惑。正因为如此,我个人从来没有理由在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).
  • 而echo和print是语言结构,print_r()和var_dump()/var_export()是常规函数。您不需要用括号将参数括起来以进行echo或print(如果您确实使用它们,它们将会像在表达式中那样被处理)。
  • 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.
  • 虽然var_export()返回有效的PHP代码,允许稍后读取值,但由于需要使用eval(),因此可以更容易地引入安全漏洞。最好使用像JSON这样的格式来存储和读取返回值。速度将是可比性的。

#3


15  

Just to add to John's answer, echo should be the only one you use to print content to the page.

为了增加John的答案,echo应该是唯一一个你用来打印内容的页面。

print is slightly slower. var_dump() and print_r() should only be used to debug.

印刷也稍慢。var_dump()和print_r()只能用于调试。

Also worth mentioning is that print_r() and var_dump() will echo by default, add a second argument to print_r() at least that evaluates to true to get it to return instead, e.g. print_r($array, TRUE).

同样值得一提的是,print_r()和var_dump()将在默认情况下echo,在print_r()中添加第二个参数,至少该值为true以使其返回,例如print_r($array, true)。

The difference between echoing and returning are:

回声与返回的区别是:

  • echo: Will immediately print the value to the output.
  • echo:将立即将值打印到输出。
  • returning: Will return the function's output as a string. Useful for logging, etc.
  • 返回:将函数的输出作为字符串返回。用于日志记录等。

#4


4  

echo

Not having return type

没有返回类型

print

Have return type

有返回类型

print_r()

Outputs as formatted,

输出格式化,

#5


4  

Echo :

回音:

It is statement not a function No return value

它是语句,不是函数没有返回值。

Not Required the parentheses

不需要括号

Not Print Array

不打印数组

Print

打印

It is real function

它是真实的函数

Return type 1

返回类型1

Required the Parentheses

需要括号

Not Print Array

不打印数组

Print_r

Print_r

Print in human readable format

以人类可读的格式打印。

String not in Quotes

字符串不报价

Not Detail Information of Variable like type and all

不包括类型等变量的详细信息。

var_dump

var_dump

All dump information of variable like type of element and sub element

所有变量的转储信息,如元素类型和子元素。

#6


2  

echo : echo is a language construct where there is not required to use parentheses with it and it can take any number of parameters and return void.

echo: echo是一种语言结构,在这里不需要使用括号,它可以接收任意数量的参数并返回void。

   void echo (param1,param2,param3.....);

   Example: echo "test1","test2,test3";

print : it is a language construct where there is not required to use parentheses it just take one parameter and return

print:它是一种语言结构,不需要使用括号,只需使用一个参数并返回。

    1 always.

           int print(param1);

           print "test1";
           print "test1","test2"; // It will give syntax error

prinf : It is a function which takes atleast one string and format style and returns length of output string.

prinf:它是一个至少需要一个字符串和格式样式并返回输出字符串长度的函数。

    int printf($string,$s);

    $s= "Shailesh";
    $i= printf("Hello %s how are you?",$s);    
    echo $i;

    Output : Hello Shailesh how are you?
             27



   echo returns void so its execution is faster than print and printf

#7


1  

**Echocan accept multiple expressions while print cannot. The Print_r () PHP function is used to return an array in a human readable form. It is simply written as

**Echocan接受多个表达式,而print不能。Print_r () PHP函数用于以人类可读的形式返回数组。它只是简单地写成。

![Print_r ($your_array)][1]

#8


1  

print_r() is used for printing the array in human readable format.

print_r()用于以人类可读的格式打印数组。

#9


0  

they both are language constructs. echo returns void and print returns 1. echo is considered slightly faster than print.

它们都是语言结构。echo返回void,打印返回1。echo被认为比印刷品稍快一些。

#10


0  

print_r() can print out value but also if second flag parameter is passed and is TRUE - it will return printed result as string and nothing send to standard output. About var_dump. If XDebug debugger is installed so output results will be formatted in much more readable and understandable way.

print_r()可以打印出值,但是如果传递了第二个标志参数,它将返回打印的结果作为字符串,而没有发送到标准输出。var_dump。如果安装了XDebug调试器,那么输出结果将以更可读和可理解的方式格式化。

#1


171  

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.)

打印和回波或多或少是相同的;它们都是显示字符串的语言结构。差异是细微的:print的返回值为1,因此可以在表达式中使用,而echo具有void返回类型;echo可以使用多个参数,虽然这种用法很少见;echo的速度略快于print。(就我个人而言,我总是使用echo,从不打印。)

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:

根据我的经验,在调试时,var_dump通常比print_r更有用。当您不知道您的变量中有哪些值/类型时,它特别有用。考虑一下这个测试程序:

$values = array(0, 0.0, false, '');

var_dump($values);
print_r ($values);

With print_r you can't tell the difference between 0 and 0.0, or false and '':

对于print_r,您不能区分0和0.0,或false和“:

array(4) {
  [0]=>
  int(0)
  [1]=>
  float(0)
  [2]=>
  bool(false)
  [3]=>
  string(0) ""
}

Array
(
    [0] => 0
    [1] => 0
    [2] => 
    [3] => 
)

#2


86  

echo

回声

  • Outputs one or more strings separated by commas
  • 输出一个或多个用逗号分隔的字符串。
  • No return value

    没有返回值

    e.g. echo "String 1", "String 2"

    例如,echo "String 1", "String 2"

print

打印

  • Outputs only a single string
  • 只输出一个字符串。
  • Returns 1, so it can be used in an expression

    返回1,因此可以在表达式中使用。

    e.g. print "Hello"

    如打印“你好”

    or, if ($expr && print "foo")

    或者,if ($expr && print "foo")

print_r()

print_r()

  • Outputs a human-readable representation of any one value
  • 输出一个人可读的任何一个值的表示。
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读。
  • Useful when debugging
  • 当调试有用
  • May return its output as a return value (instead of echoing) if the second optional argument is given
  • 如果给出第二个可选参数,可以将其输出返回为返回值(而不是echo) ?

var_dump()

var_dump()

  • Outputs a human-readable representation of one or more values separated by commas
  • 输出以逗号分隔的一个或多个值的可读表示。
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读。
  • Uses a different output format to print_r(), for example it also prints the type of values
  • 使用与print_r()不同的输出格式,例如,它也打印值的类型。
  • Useful when debugging
  • 当调试有用
  • No return value
  • 没有返回值

var_export()

var_export()

  • Outputs a human-readable and PHP-executable representation of any one value
  • 输出任何一个值的可读和可执行的表示。
  • Accepts not just strings but other types including arrays and objects, formatting them to be readable
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读。
  • Uses a different output format to both print_r() and var_dump() - resulting output is valid PHP code!
  • 对print_r()和var_dump()使用不同的输出格式,结果输出是有效的PHP代码!
  • Useful when debugging
  • 当调试有用
  • May return its output as a return value (instead of echoing) if the second optional argument is given
  • 如果给出第二个可选参数,可以将其输出返回为返回值(而不是echo) ?

Notes:

注:

  • 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.
  • 尽管可以在表达式中使用print,但我建议人们避免这样做,因为它不利于代码可读性(而且因为它不太可能有用)。当它与其他操作符交互时,优先规则也会令人迷惑。正因为如此,我个人从来没有理由在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).
  • 而echo和print是语言结构,print_r()和var_dump()/var_export()是常规函数。您不需要用括号将参数括起来以进行echo或print(如果您确实使用它们,它们将会像在表达式中那样被处理)。
  • 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.
  • 虽然var_export()返回有效的PHP代码,允许稍后读取值,但由于需要使用eval(),因此可以更容易地引入安全漏洞。最好使用像JSON这样的格式来存储和读取返回值。速度将是可比性的。

#3


15  

Just to add to John's answer, echo should be the only one you use to print content to the page.

为了增加John的答案,echo应该是唯一一个你用来打印内容的页面。

print is slightly slower. var_dump() and print_r() should only be used to debug.

印刷也稍慢。var_dump()和print_r()只能用于调试。

Also worth mentioning is that print_r() and var_dump() will echo by default, add a second argument to print_r() at least that evaluates to true to get it to return instead, e.g. print_r($array, TRUE).

同样值得一提的是,print_r()和var_dump()将在默认情况下echo,在print_r()中添加第二个参数,至少该值为true以使其返回,例如print_r($array, true)。

The difference between echoing and returning are:

回声与返回的区别是:

  • echo: Will immediately print the value to the output.
  • echo:将立即将值打印到输出。
  • returning: Will return the function's output as a string. Useful for logging, etc.
  • 返回:将函数的输出作为字符串返回。用于日志记录等。

#4


4  

echo

Not having return type

没有返回类型

print

Have return type

有返回类型

print_r()

Outputs as formatted,

输出格式化,

#5


4  

Echo :

回音:

It is statement not a function No return value

它是语句,不是函数没有返回值。

Not Required the parentheses

不需要括号

Not Print Array

不打印数组

Print

打印

It is real function

它是真实的函数

Return type 1

返回类型1

Required the Parentheses

需要括号

Not Print Array

不打印数组

Print_r

Print_r

Print in human readable format

以人类可读的格式打印。

String not in Quotes

字符串不报价

Not Detail Information of Variable like type and all

不包括类型等变量的详细信息。

var_dump

var_dump

All dump information of variable like type of element and sub element

所有变量的转储信息,如元素类型和子元素。

#6


2  

echo : echo is a language construct where there is not required to use parentheses with it and it can take any number of parameters and return void.

echo: echo是一种语言结构,在这里不需要使用括号,它可以接收任意数量的参数并返回void。

   void echo (param1,param2,param3.....);

   Example: echo "test1","test2,test3";

print : it is a language construct where there is not required to use parentheses it just take one parameter and return

print:它是一种语言结构,不需要使用括号,只需使用一个参数并返回。

    1 always.

           int print(param1);

           print "test1";
           print "test1","test2"; // It will give syntax error

prinf : It is a function which takes atleast one string and format style and returns length of output string.

prinf:它是一个至少需要一个字符串和格式样式并返回输出字符串长度的函数。

    int printf($string,$s);

    $s= "Shailesh";
    $i= printf("Hello %s how are you?",$s);    
    echo $i;

    Output : Hello Shailesh how are you?
             27



   echo returns void so its execution is faster than print and printf

#7


1  

**Echocan accept multiple expressions while print cannot. The Print_r () PHP function is used to return an array in a human readable form. It is simply written as

**Echocan接受多个表达式,而print不能。Print_r () PHP函数用于以人类可读的形式返回数组。它只是简单地写成。

![Print_r ($your_array)][1]

#8


1  

print_r() is used for printing the array in human readable format.

print_r()用于以人类可读的格式打印数组。

#9


0  

they both are language constructs. echo returns void and print returns 1. echo is considered slightly faster than print.

它们都是语言结构。echo返回void,打印返回1。echo被认为比印刷品稍快一些。

#10


0  

print_r() can print out value but also if second flag parameter is passed and is TRUE - it will return printed result as string and nothing send to standard output. About var_dump. If XDebug debugger is installed so output results will be formatted in much more readable and understandable way.

print_r()可以打印出值,但是如果传递了第二个标志参数,它将返回打印的结果作为字符串,而没有发送到标准输出。var_dump。如果安装了XDebug调试器,那么输出结果将以更可读和可理解的方式格式化。