PHP 中的'print' 和'echo' 之间有什么区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006586/
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 00:37:02  来源:igfitidea点击:

Is there any difference between 'print' and 'echo' in PHP?

php

提问by Peter

Possible Duplicate:
How are echo and print different in PHP?

可能的重复:
PHP 中的 echo 和 print 有何不同?

UPDATE :

更新 :

I found to my relief an exactduplicate(it wasn't showing up when I typed this question at first, I found it with ... google): Please vote with me to close this question, because it's tiring, go hunt that other poor guy a bit ;-)

令我欣慰的是,我发现了一个完全相同的重复项(起初我输入这个问题时它没有出现,我用...谷歌找到了它):请和我一起投票关闭这个问题,因为它很累,去寻找其他的可怜的家伙有点;-)



Is there any difference between printand echoin PHP? If so, which should I use and when? If not, why are there two keywords?

PHP 中的print和之间有什么区别echo吗?如果是这样,我应该使用哪个以及何时使用?如果没有,为什么有两个关键字?

UPDATE :

更新 :

At the downvoters : please read the SO faq. SO was setup also to capture googleable questions. so you shouldn't downvote for that, this question is a valid question, answered on a lot of places and now on SO too.

在投票者中:请阅读 SO 常见问题解答。SO 还设置为捕获可搜索的问题。所以你不应该为此投反对票,这个问题是一个有效的问题,在很多地方都有回答,现在也在 SO 上。

Of course you can downvote for another reason, but please leave a comment in the lines of -1 : downvoted for .. , cause for now, I'm not understanding the downvotes.

当然,您可以出于其他原因投反对票,但请在 -1 行中发表评论:投反对票为 .. ,因为现在,我不理解投反对票。

回答by karim79

From this link, suggested by the PHP manual entry for the echo()function:

这个链接echo()函数PHP 手册条目建议:

  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.

  2. Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be

  3. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

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

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

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

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 ("and a 123"); print "and a 123";

  1. 速度。两者之间存在差异,但速度方面应该与您使用哪一个无关。echo 稍微快一点,因为如果您真的想深入了解它,它不会设置返回值。

  2. 表达。print() 就像一个函数,你可以这样做: $ret = print "Hello World"; 而 $ret 将是

  3. 这意味着 print 可以用作更复杂的表达式的一部分,而 echo 不能。PHP手册中的一个例子:

    $b ? 打印“真”:打印“假”;

print 也是优先级表的一部分,如果要在复杂的表达式中使用它,它需要是优先级表的一部分。不过,它位于优先级列表的底部。只有“,” AND、OR 和 XOR 较低。

  1. 参数)。语法是:echo expression [, expression[, expression] ... ] 但是echo ( expression, expression ) 无效。这将是有效的: echo ("howdy"),("partner"); 同:echo "howdy","partner";
    (把括号放在那个简单的例子中是没有意义的,因为像这样的单个术语没有运算符优先级问题。)

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

echo "和", 1, 2, 3; // 逗号分隔,不带括号
echo ("and a 123"); // 只有一个带括号的参数

print() 只能带一个参数:

打印(“和一个 123”);打印“和一个 123”;

回答by NJChim

some say echo is slightly faster than print since it has no return value. though here is someone who doesn't think the speed difference matters much... http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster

有人说 echo 比 print 稍快,因为它没有返回值。尽管这里有人认为速度差异并不重要...... http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster

回答by anddoutoi

print returns, echo does not.

打印返回,回显不返回。

And you are right, totally googleable.

你是对的,完全可以谷歌搜索。