在PHP中echo和print有什么不同?(复制)

时间:2022-03-27 03:53:59

Possible Duplicate:
Reference: Comparing PHP's print and echo

可能的重复:引用:比较PHP的打印和echo

Is there any major and fundamental difference between these two functions in PHP?

这两个函数在PHP中有什么主要和根本的区别吗?

5 个解决方案

#1


252  

From: http://web.archive.org/web/20090221144611/http://faqts.com/knowledge_base/view.phtml/aid/1/fid/40

来自:http://web.archive.org/web/20090221144611/http:/ /faqts.com/knowledge_base/view.phtml/aid/1/fid/40

  1. Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

    速度。两者之间是有区别的,但就速度而言,使用哪一种应该无关紧要。echo的速度稍快一些,因为如果你真的想要深入细节的话,它不会设置返回值。

  2. Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

    表达式。print()的行为类似于一个函数,您可以这样做:$ret =打印“Hello World”;$ret等于1。这意味着打印可以作为一个更复杂的表达式的一部分使用,而echo不能。一个来自PHP手册的示例:

$b ? print "true" : print "false";

print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower.

如果要在一个复杂的表达式中使用print,那么它也是优先级表的一部分。但它只是在优先列表的底部。只有","和,或和x较低。

  1. Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner"; (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)
  2. 参数(s)。语法是:echo expression[, expression[, expression]…但是echo(表达式,表达式)是无效的。这将是有效的:echo(“howdy”)、(“partner”);同:echo "howdy","partner";(将括号放在那个简单示例中没有任何意义,因为这样的单个术语没有优先级问题。)

So, echo without parentheses can take multiple parameters, which get concatenated:

因此,没有括号的echo可以使用多个参数,这些参数被连接:

   echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
   echo ("and a 123");        // just one parameter with parentheses

print() can only take one parameter:

print()只能接受一个参数:

   print ("and a 123");
   print  "and a 123";

#2


65  

They are:

它们是:

  • print only takes one parameter, while echo can have multiple parameters.
  • 打印只接受一个参数,而echo可以有多个参数。
  • print returns a value (1), so can be used as an expression.
  • print返回一个值(1),因此可以用作表达式。
  • echo is slightly faster.
  • 回声稍快。

#3


8  

To add to the answers above, while print can only take one parameter, it will allow for concatenation of multiple values, ie:

要添加到上面的答案中,虽然print只能接受一个参数,但它将允许连接多个值,即:

$count = 5;

print "This is " . $count . " values in " . $count/5 . " parameter";

This is 5 values in 1 parameter

这是一个参数中的5个值。

#4


6  

As the PHP.net manual suggests, take a read of this discussion.

正如PHP.net手册所建议的,请阅读本讨论。

One major difference is that echo can take multiple parameters to output. E.g.:

一个主要的区别是echo可以输出多个参数。例如:

echo 'foo', 'bar';   // Concatenates the 2 strings
print('foo', 'bar'); // Fatal error

If you're looking to evaluate the outcome of an output statement (as below) use print. If not, use echo.

如果您希望评估输出语句(如下所示)的结果,请使用print。如果没有,使用回声。

$res = print('test');
var_dump($res); //bool(true)

#5


5  

I think print() is slower than echo.

我认为print()比echo慢。

I like to use print() only for situations like:

我喜欢用print()只适用于以下情况:

 echo 'Doing some stuff... ';
 foo() and print("ok.\n") or print("error: " . getError() . ".\n");

#1


252  

From: http://web.archive.org/web/20090221144611/http://faqts.com/knowledge_base/view.phtml/aid/1/fid/40

来自:http://web.archive.org/web/20090221144611/http:/ /faqts.com/knowledge_base/view.phtml/aid/1/fid/40

  1. Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

    速度。两者之间是有区别的,但就速度而言,使用哪一种应该无关紧要。echo的速度稍快一些,因为如果你真的想要深入细节的话,它不会设置返回值。

  2. Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

    表达式。print()的行为类似于一个函数,您可以这样做:$ret =打印“Hello World”;$ret等于1。这意味着打印可以作为一个更复杂的表达式的一部分使用,而echo不能。一个来自PHP手册的示例:

$b ? print "true" : print "false";

print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower.

如果要在一个复杂的表达式中使用print,那么它也是优先级表的一部分。但它只是在优先列表的底部。只有","和,或和x较低。

  1. Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner"; (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)
  2. 参数(s)。语法是:echo expression[, expression[, expression]…但是echo(表达式,表达式)是无效的。这将是有效的:echo(“howdy”)、(“partner”);同:echo "howdy","partner";(将括号放在那个简单示例中没有任何意义,因为这样的单个术语没有优先级问题。)

So, echo without parentheses can take multiple parameters, which get concatenated:

因此,没有括号的echo可以使用多个参数,这些参数被连接:

   echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
   echo ("and a 123");        // just one parameter with parentheses

print() can only take one parameter:

print()只能接受一个参数:

   print ("and a 123");
   print  "and a 123";

#2


65  

They are:

它们是:

  • print only takes one parameter, while echo can have multiple parameters.
  • 打印只接受一个参数,而echo可以有多个参数。
  • print returns a value (1), so can be used as an expression.
  • print返回一个值(1),因此可以用作表达式。
  • echo is slightly faster.
  • 回声稍快。

#3


8  

To add to the answers above, while print can only take one parameter, it will allow for concatenation of multiple values, ie:

要添加到上面的答案中,虽然print只能接受一个参数,但它将允许连接多个值,即:

$count = 5;

print "This is " . $count . " values in " . $count/5 . " parameter";

This is 5 values in 1 parameter

这是一个参数中的5个值。

#4


6  

As the PHP.net manual suggests, take a read of this discussion.

正如PHP.net手册所建议的,请阅读本讨论。

One major difference is that echo can take multiple parameters to output. E.g.:

一个主要的区别是echo可以输出多个参数。例如:

echo 'foo', 'bar';   // Concatenates the 2 strings
print('foo', 'bar'); // Fatal error

If you're looking to evaluate the outcome of an output statement (as below) use print. If not, use echo.

如果您希望评估输出语句(如下所示)的结果,请使用print。如果没有,使用回声。

$res = print('test');
var_dump($res); //bool(true)

#5


5  

I think print() is slower than echo.

我认为print()比echo慢。

I like to use print() only for situations like:

我喜欢用print()只适用于以下情况:

 echo 'Doing some stuff... ';
 foo() and print("ok.\n") or print("error: " . getError() . ".\n");