PHP 中的 echo 和 print 有何不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/234241/
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
How are echo and print different in PHP?
提问by zuk1
Possible Duplicate:
Reference: Comparing PHP's print and echo
可能的重复:
参考:比较 PHP 的打印和回显
Is there any major and fundamental difference between these two functions in PHP?
PHP 中的这两个函数之间有什么主要和根本的区别吗?
回答by dl__
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
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.
Expression.
print()behaves like a function in that you can do:$ret = print "Hello World"; And$retwill be1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:
速度。两者之间存在差异,但速度方面应该与您使用哪一个无关。echo 稍微快一点,因为如果您真的想深入了解它,它不会设置返回值。
表达。
print()行为就像在你可以做一个函数:$ret = print "Hello World"; 并且$ret会1。这意味着 print 可以用作更复杂的表达式的一部分,而 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 ,ANDORXORare lower.
print 也是优先级表的一部分,如果要在复杂的表达式中使用它,它需要是优先级表的一部分。不过,它位于优先级列表的底部。只有,ANDORXOR更低。
- Parameter(s). The grammar is:
echo expression [, expression[, expression] ... ]Butecho ( 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.)
- 参数)。语法是:
echo expression [, expression[, expression] ... ]但echo ( expression, expression )无效。这将是有效的: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";
回答by seanyboy
They are:
他们是:
- print only takes one parameter, while echo can have multiple parameters.
- print returns a value (1), so can be used as an expression.
- echo is slightly faster.
- print 只需要一个参数,而 echo 可以有多个参数。
- print 返回值 (1),因此可以用作表达式。
- echo 稍微快一点。
回答by seanyboy
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
这是 1 个参数中的 5 个值
回答by grilix
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");
回答by Ross
As the PHP.net manual suggests, take a read of this discussion.
正如 PHP.net 手册所建议的,请阅读此讨论。
One major difference is that echocan 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. 如果没有,请使用echo.
$res = print('test');
var_dump($res); //bool(true)

