php 与 echo 与 return 连接时句号和逗号之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1466408/
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
Difference between period and comma when concatenating with echo versus return?
提问by omg
I just found that this will work:
我刚刚发现这会起作用:
echo $value , " continue";
but this does not:
但这不会:
return $value , " continue";
While "." works in both.
尽管 ”。” 两者都适用。
What is the difference between a period and a comma here?
这里的句号和逗号有什么区别?
回答by Gumbo
returndoes only allow one single expression. But echoallows a list of expressions where each expression is separated by a comma. But note that since echois not a function but a special language construct, wrapping the expression list in parenthesis is illegal.
return只允许一个单一的表达。但是echo允许一个表达式列表,其中每个表达式用逗号分隔。但请注意,由于echo它不是函数而是一种特殊的语言结构,因此将表达式列表括在括号中是非法的。
回答by Mr.Web
You also have to note that echoas a construct is faster with commasthan it is with dots.
您还必须注意,echo作为一个构造,使用逗号比使用点更快。
So if you join a character 4 million times this is what you get:
所以如果你加入一个角色 400 万次,你会得到:
echo $str1, $str2, $str3;
About 2.08seconds
echo $str1 . $str2 . $str3;
About 3.48seconds
回声 $str1, $str2, $str3;
约2.08秒
回声 $str1 。$str2 。$str3;
约3.48秒
It almost takes half the time as you can see above.
正如您在上面看到的那样,它几乎需要一半的时间。
This is because PHP with dots joins the string firstand then outputs them, while with commas just prints them out one after the other.
这是因为PHP用点连接字符串第一然后对其进行输出,同时用逗号刚刚打印出来一前一后。
We are talking about fractions, but still.
我们在谈论分数,但仍然如此。
回答by GSto
The .is the concatenation operator in PHP, for putting two strings together.
的.是PHP的连接运算符,用于将两个字符串连接在一起。
The comma can be used for multiple inputs to echo.
逗号可用于对 的多个输入echo。
回答by Patrick Desjardins
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.
点 ( .) 用于连接变量或字符串。这就是为什么当您在连接两个字符串时回显时它起作用的原因,当您在方法中返回字符串的串联时它起作用。但是逗号不会连接,这就是 return 语句不起作用的原因。
echois a language construct that can take multiple expressions which is why the comma works:
echo是一种可以采用多个表达式的语言结构,这就是逗号起作用的原因:
void echo ( string $arg1 [, string $... ] )
Use the dot for concatenation.
使用点进行连接。
回答by knittl
echois a language construct (nota function) and can take multiple arguments, that's why ,works. using comma will be slightly even (but only some nanoseconds, nothing to worry about)
echo是一种语言结构(不是函数)并且可以接受多个参数,这就是为什么,有效。使用逗号会稍微均匀(但只有几纳秒,无需担心)
.is the concatenation operator (the glue) for strings
.是字符串的连接运算符(胶水)
回答by Kibbee
echois 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.
echo实际上是一个函数(不是真的,但假设它是为了参数),它接受任意数量的参数并将它们连接在一起。
While returnis 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 the returnstatement.
Whilereturn不是一个函数,而是一个关键字,它告诉函数返回值,它试图解释,为某种运算符。.在使用return语句的情况下,您应该将其用作连接运算符。
回答by Rain
It's worth mentioning that the concatenation operator .has a higher precedence than lots of other operators and has equal precedence with +and -operators
值得一提的是,连接运算符.的优先级高于许多其他运算符,并且与+和-运算符的优先级相同
Why this is important?
为什么这很重要?
Well, talk is cheap let me show you the code ( from PHP documentation)
好吧,话不多说,让我向您展示代码(来自PHP 文档)
$x = 4;
// this line might result in unexpected output:
echo "x minus one equals " . $x-1 . ", or so I hope\n";
// because it is evaluated like this line:
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
// the desired precedence can be enforced by using parentheses:
echo "x minus one equals " . ($x-1) . ", or so I hope\n";
In fact, the first line will issue a deprecation message as of PHP 7.4.0
实际上,从 PHP 7.4.0 开始,第一行将发出弃用消息
Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence
不推荐使用:包含两个 '.' 的无括号表达式的行为 '+'/'-' 将在 PHP 8 中改变:'+'/'-' 将具有更高的优先级
So in PHP 8 it seems the problem of associativity in this case will be solved by giving +and -operators a higher precedence.
所以在 PHP 8 中,这种情况下的结合性问题似乎可以通过给+和-运算符更高的优先级来解决。
So can we say now that .and ,when using echo give the same result?
所以,我们可以说:现在.和,使用echo给出相同的结果是什么时候?
No, they will not always give the same result
不,他们不会总是给出相同的结果
Let's take this case for example
我们以这个案例为例
echo ' Here\'s ' . $name ?? 'Johnny';
Here we used the Null coalescing operatorso 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 Johnnysince $name is not defined or so they hope.
这里我们使用了Null 合并运算符,所以如果 $name 存在并且不是 NULL,它将被返回,否则它返回 Johnny。乍一看,人们可能认为结果是Here's Johnny,因为 $name 未定义,或者他们希望如此。
Actually the result will be
其实结果会是
PHP Notice: Undefined variable: name
Here's
What happened here is that ??operator has a lower precedence than the .which means PHP will try to evaluate (Here's $name)first.
这里发生的事情是??运算符的优先级低于 the .,这意味着 PHP 将首先尝试评估(这是 $name)。
You can solve this by either enclosing the expression in parentheses
您可以通过将表达式括在括号中来解决此问题
echo ' Here\'s ' . ($name ?? 'Johnny');
Or simply use a comma.
或者干脆使用逗号。
echo ' Here\'s ' , $name ?? 'Johnny';

