Possible Duplicate:
How are echo and print different in PHP?可能重复:PHP中的echo和print有何不同?
UPDATE :
I found to my relief an exact duplicate (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 print
and echo
in 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.
在downvoters:请阅读SO faq。设置也是为了捕捉谷歌问题。因此,你不应该为此投票,这个问题是一个有效的问题,在很多地方都有答案,现在也是如此。
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的行中留下评论:为...投票,因为现在,我不理解downvotes。
4 个解决方案
#1
From this link, suggested by the PHP manual entry for the echo()
function:
从这个链接,由echo手册条目建议echo()函数:
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 $ret will be
表达。 print()的行为类似于一个函数,你可以这样做:$ ret = print“Hello World”; $ ret将是
That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:
这意味着print可以用作echo不能表达的更复杂表达式的一部分。 PHP手册中的一个例子:
$b ? print "true" : print "false";
$ 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,它也是优先级表的一部分。它只是在优先列表的底部。只有“,”AND,OR和XOR较低。
- 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.)参数(S)。语法是:echo表达式[,表达式[,表达式] ...]但是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 parenthesesecho“和a”,1,2,3; //逗号分隔,没有括号echo(“和123”); //只有一个带括号的参数
print() can only take one parameter:
print()只能接受一个参数:
print ("and a 123"); print "and a 123";
打印(“和123”);打印“和123”;
#2
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
有人说回声比印刷略快,因为它没有返回值。虽然这里有人不认为速度差异很重要... http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster
#4
print returns, echo does not.
打印返回,echo不。
And you are right, totally googleable.
而你是对的,完全可以谷歌。
#1
From this link, suggested by the PHP manual entry for the echo()
function:
从这个链接,由echo手册条目建议echo()函数:
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 $ret will be
表达。 print()的行为类似于一个函数,你可以这样做:$ ret = print“Hello World”; $ ret将是
That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:
这意味着print可以用作echo不能表达的更复杂表达式的一部分。 PHP手册中的一个例子:
$b ? print "true" : print "false";
$ 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,它也是优先级表的一部分。它只是在优先列表的底部。只有“,”AND,OR和XOR较低。
- 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.)参数(S)。语法是:echo表达式[,表达式[,表达式] ...]但是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 parenthesesecho“和a”,1,2,3; //逗号分隔,没有括号echo(“和123”); //只有一个带括号的参数
print() can only take one parameter:
print()只能接受一个参数:
print ("and a 123"); print "and a 123";
打印(“和123”);打印“和123”;
#2
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
有人说回声比印刷略快,因为它没有返回值。虽然这里有人不认为速度差异很重要... http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster
#3
答案在文档中。
#4
print returns, echo does not.
打印返回,echo不。
And you are right, totally googleable.
而你是对的,完全可以谷歌。