与 echo 和 return 连接时句号和逗号之间的区别?
我刚刚发现这会起作用:
echo $value , " continue";
但这不起作用:
return $value , " continue";
虽然 .
可以代替 ,
在 echo
和 return< /代码> 语句。
这里的句号和逗号有什么区别?
I just found that this will work:
echo $value , " continue";
but this does not:
return $value , " continue";
While .
works instead of ,
in both the echo
and return
statements.
What is the difference between a period and a comma here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
return
只允许一个表达式,但echo
允许表达式列表,其中每个表达式以逗号分隔。但请注意,由于
echo
不是一个函数,而是一种特殊的语言结构,因此将表达式列表括在括号中是非法的。return
only allows one expression, butecho
allows a list of expressions where each expression is separated by a comma.But note that since
echo
is not a function but a special language construct, wrapping the expression list in parenthesis is illegal.您还必须注意,
echo
作为一种构造,使用逗号比使用点更快。因此,如果您加入某个角色 400 万次,您将得到以下结果:
几乎需要您在上面看到的一半时间。
这是因为带有点的 PHP 会首先连接字符串,然后输出它们,而带有逗号的 PHP 只是将它们一个接一个地打印出来。
我们谈论的是几分之一秒,但仍然如此。
原始来源
You also have to note that
echo
as a construct is faster with commas than it is with dots.So if you join a character 4 million times this is what you get:
It almost takes half the time as you can see above.
This is because PHP with dots joins the string first and then outputs them, while with commas just prints them out one after the other.
We are talking about fractions of a second, but still.
Original Source
.
是 PHP 中的连接运算符,用于将两个字符串连接在一起。逗号可用于
echo
的多个输入。The
.
is the concatenation operator in PHP, for putting two strings together.The comma can be used for multiple inputs to
echo
.点 (
.
) 用于连接变量或字符串。这就是为什么当您在连接两个字符串时进行回显时它会起作用,并且当您在方法中返回字符串的串联时它会起作用。但是逗号没有连接,这就是 return 语句不起作用的原因。echo
是一种可以采用多个表达式的语言构造,这就是逗号起作用的原因:使用点进行连接。
Dot (
.
) is for concatenation of a variable or string. This is why it works when you echo while concatenating two strings, and it works when you return a concatenation of a string in a method. But the comma doesn't concatenate and this is why the return statement won't work.echo
is a language construct that can take multiple expressions which is why the comma works:Use the dot for concatenation.
echo
是一种语言构造(不是函数),可以接受多个参数,这就是,
起作用的原因。使用逗号会稍微均匀(但只有几纳秒,无需担心).
是字符串的连接运算符(粘合剂)echo
is a language construct (not a function) and can take multiple arguments, that's why,
works. using comma will be slightly even (but only some nanoseconds, nothing to worry about).
is the concatenation operator (the glue) for stringsecho
实际上是一个函数(不是真的,但我们可以说它是为了为了论证),它接受任意数量的参数并将它们连接在一起。虽然
return
不是一个函数,而是一个关键字,它告诉函数返回值,并且它试图将,
解释为某种运算符。当您使用return
语句时,您应该使用.
作为串联运算符。echo
is actually a function (not really, but let's say it is for the sake of argument) that takes any number of parameters and will concatenate them together.While
return
is not a function, but rather a keyword, that tells the function to return the value, and it is trying to interpret,
as some kind of operator. You should be using.
as the concatenation operator in the case when you are using thereturn
statement.值得一提的是,连接运算符
.
比许多其他运算符具有更高的优先级,并且与+
和-
运算符具有相同的优先级为什么这很重要?
好吧,空谈是廉价的,让我向您展示代码(来自 PHP 文档)
事实上,从 PHP 7.4.0 开始,第一行将发出弃用消息
因此,在 PHP 8 中,这种情况下的关联性问题似乎可以通过给予
+
和-
运算符更高的优先级来解决。那么现在我们可以说
.
和,
在使用 echo 时会给出相同的结果吗?不,它们不会总是给出相同的结果结果
让我们以这种情况为例,
这里我们使用了 空合并运算符 因此,如果 $name 存在且不为 NULL,它将被返回,否则将返回 Johnny。乍一看,人们可能会认为结果将是 Here's Johnny 因为 $name 未定义,或者他们希望如此。
实际上结果将是
这里发生的是
??
运算符的优先级低于.
这意味着 PHP 将尝试计算 (Here's $name)< /em> 首先。您可以通过将表达式括在括号中
或仅使用逗号来解决此问题。
It's worth mentioning that the concatenation operator
.
has a higher precedence than lots of other operators and has equal precedence with+
and-
operatorsWhy is this important?
Well, talk is cheap let me show you the code ( from PHP documentation)
In fact, the first line will issue a deprecation message as of PHP 7.4.0
So in PHP 8 it seems the problem of associativity in this case will be solved by giving
+
and-
operators a higher precedence.So can we say now that
.
and,
when using echo give the same result?No, they will not always give the same result
Let's take this case for example
Here we used the Null coalescing operator so if $name exists and is not NULL it'll be returned otherwise it returns Johnny. At first glance, one may think the result will be Here's Johnny since $name is not defined or so they hope.
Actually the result will be
What happened here is that
??
operator has a lower precedence than the.
which means PHP will try to evaluate (Here's $name) first.You can solve this by either enclosing the expression in parentheses
Or simply use a comma.
与 Mr.Web 的答案相比,2022 年的 PHP 的速度更快。
In contrast with Mr.Web's answer PHP in 2022 is faster with the dots.
还有另一个有趣的区别。根据 @phirschybar 对这个问题的回答,如果您将逗号与 html
使用点连接...
...您得到:
但是当使用逗号分隔值...
...您得到:
There is another interesting difference. Per @phirschybar's answer to this question, if you use commas in conjunction with html
<pre>
you get a result that maintains line breaks. This is useful for viewing a formatted object. For example, given this object:Using dot concatenation...
... you get:
But when using commas to delimit the values...
... you get: