PHP 中的 echo、print 和 print_r 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1647322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:23:20  来源:igfitidea点击:

What's the difference between echo, print, and print_r in PHP?

php

提问by Mak

I use echoand print_rmuch, and almost never use print.

我使用echoprint_r很多,几乎从不使用print.

I feel echois a macro, and print_ris an alias of var_dump.

我感觉echo是一个宏,print_rvar_dump.

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

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

回答by John Kugelman

printand echoare more or less the same; they are both language constructs that display strings. The differences are subtle: printhas a return value of 1 so it can be used in expressions whereas echohas a voidreturn type; echocan take multiple parameters, although such usage is rare; echois slightly faster than print. (Personally, I always use echo, never print.)

print并且echo或多或少相同;它们都是显示字符串的语言结构。区别很微妙:print返回值为 1,因此可以在表达式中使用,而echo具有void返回类型;echo可以接受多个参数,尽管这种用法很少见;echo比 略快print。(就个人而言,我总是使用echo,从不使用print。)

var_dumpprints 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_rprints 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_dumpis usually more useful than print_rwhen 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_dumpprint_r根据我的经验,通常比调试时更有用。当您不确切知道变量中有哪些值/类型时,它特别有用。考虑这个测试程序:

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

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

With print_ryou can't tell the difference between 0and 0.0, or falseand '':

随着print_r你不能告诉之间的区别00.0,或false''

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

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

回答by thomasrutter

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

    e.g. print "Hello"

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

  • 只输出一个字符串
  • Returns 1,因此它可以在表达式中使用

    例如 print "Hello"

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

print_r()

打印_r()

  • Outputs a human-readable representation of any onevalue
  • 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
  • 输出任何一个值的人类可读表示
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读
  • 调试时很有用
  • 如果给出第二个可选参数,则可以将其输出作为返回值(而不是回显)返回

var_dump()

var_dump()

  • Outputs a human-readable representation of one or morevalues 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 typeof values
  • Useful when debugging
  • No return value
  • 输出一个或多个由逗号分隔的值的人类可读表示
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读
  • 使用不同的输出格式print_r(),例如它还打印值的类型
  • 调试时很有用
  • 无返回值

var_export()

var_export()

  • Outputs a human-readable and PHP-executablerepresentation of any onevalue
  • 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!
  • Useful when debugging
  • May return its output as a return value (instead of echoing) if the second optional argument is given
  • 输出任何一个值的人类可读和 PHP 可执行的表示
  • 不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读
  • 使用不同的输出格式print_r()var_dump()- 结果输出是有效的 PHP 代码!
  • 调试时很有用
  • 如果给出第二个可选参数,则可以将其输出作为返回值(而不是回显)返回

Notes:

笔记:

  • Even though printcan 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 echoand printare language constructs, print_r()and var_dump()/var_export()are regular functions. You don't need parentheses to enclose the arguments to echoor 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.
  • 尽管print可以在表达式中使用,但我建议人们避免这样做,因为它不利于代码可读性(并且因为它不太可能有用)。与其他运算符交互时的优先规则也可能令人困惑。正因为如此,我个人没有理由过度使用它echo
  • echoandprint是语言结构,print_r()var_dump()/var_export()是常规函数。您不需要括号来将参数括在echoor 中print(如果您确实使用了它们,它们将像在表达式中一样对待)。
  • 虽然var_export()返回有效的 PHP 代码允许稍后读回值,但在生产代码中依赖此代码可能更容易引入安全漏洞,因为需要使用eval(). 最好使用像 JSON 这样的格式来存储和读回值。速度将是可比的。

回答by alex

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

只是为了补充约翰的答案echo应该是您用来将内容打印到页面的唯一答案

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

print稍慢。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()将被默认回声,添加第二个论点print_r()至少一个评估为真的让它返回相反,如print_r($array, TRUE)

The difference between echoing and returning are:

回显和返回的区别是:

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

回答by user1899888

echo

Not having return type

没有返回类型

print

Have return type

有返回类型

print_r()

Outputs as formatted,

输出格式,

回答by N Randhawa

The difference between echo, print, print_rand var_dumpis very simple.

echoprintprint_rvar_dump之间的区别非常简单。

echo

回声

echois actually not a function but a language construct which is used to print output. It is marginally faster than the print.

echo实际上不是一个函数,而是一个用于打印输出的语言结构。它比打印略快。

echo "Hello World";    // this will print Hello World
echo "Hello ","World"; // Multiple arguments - this will print Hello World

$var_1=55;
echo "$var_1";               // this will print 55
echo "var_1=".$var_1;        // this will print var_1=55
echo 45+$var_1;              // this will print 100

$var_2="PHP";
echo "$var_2";                   // this will print PHP

$var_3=array(99,98,97)           // Arrays are not possible with echo (loop or index  value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with echo (loop or index  value required)

You can also use echo statement with or without parenthese

您还可以使用带或不带括号的 echo 语句

echo ("Hello World");   // this will print Hello World

print

打印

Just like echoconstruct printis also a language construct and not a real function. The differences between echoand printis that printonly accepts a single argument and printalways returns 1. Whereas echohas no return value. So printstatement can be used in expressions.

就像echo构造一样,print也是一种语言构造,而不是真正的函数。之间的差别回波打印打印只接受一个参数,并打印总是返回1.鉴于回声没有返回值。所以print语句可以用在表达式中。

print "Hello World";    // this will print Hello World
print "Hello ","World"; // Multiple arguments - NOT POSSIBLE with print
$var_1=55;
print "$var_1";               // this will print 55
print "var_1=".$var_1;        // this will print var_1=55
print 45+$var_1;              // this will print 100

$var_2="PHP";
print "$var_2";                   // this will print PHP

$var_3=array(99,98,97)           // Arrays are not possible with print (loop or index  value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with print (loop or index  value required)

Just like echo, printcan be used with or without parentheses.

就像echo一样, print可以带或不带括号使用。

print ("Hello World");   // this will print Hello World

print_r

打印_r

The print_r()function is used to print human-readable information about a variable. If the argument is an array, print_r()function prints its keys and elements (same for objects).

所述的print_r()函数是用来打印人可读的一个变量的信息。如果参数是一个数组,print_r()函数打印它的键和元素(对象相同)。

print_r ("Hello World");    // this will print Hello World

$var_1=55;
print_r ("$var_1");               // this will print 55
print_r ("var_1=".$var_1);        // this will print var_1=55
print_r (45+$var_1);              // this will print 100

$var_2="PHP";
print_r ("$var_2");                // this will print PHP

$var_3=array(99,98,97)             // this will print Array ( [0] => 1 [1] => 2 [2] => 3 ) 
$var_4=array("P"=>"3","J"=>"4");   // this will print  Array ( [P] => 3 [J] => 4 ) 

var_dump

变量转储

var_dumpfunction usually used for debugging and prints the information ( type and value) about a variable/array/object.

var_dump函数通常用于调试和打印有关变量/数组/对象的信息(类型和值)。

var_dump($var_1);     // this will print  int(5444) 
var_dump($var_2);     // this will print  string(5) "Hello" 
var_dump($var_3);     // this will print  array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } 
var_dump($var_4);     // this will print  array(2) { ["P"]=> string(1) "3" ["J"]=> string(1) "4" }

回答by UnderGround

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

打印_r

Print in human readable format

以人类可读的格式打印

String not in Quotes

字符串不在引号中

Not Detail Information of Variable like type and all

不是变量的详细信息,如类型和所有

var_dump

变量转储

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

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

回答by shaik afroz

**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]

回答by Shailesh Thapa

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

回答by vaibhav kulkarni

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

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

回答by Rinat

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() 可以打印出值,但如果第二个标志参数被传递并且为 TRUE - 它将以字符串形式返回打印结果,并且没有任何内容发送到标准输出。关于 var_dump。如果安装了 XDebug 调试器,那么输出结果将以更易读和易懂的方式格式化。