问题
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?
回答1:
return does only allow one single expression. But echo 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.
回答2:
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:
echo $str1, $str2, $str3;
About 2.08 seconds
echo $str1 . $str2 . $str3;
About 3.48 seconds
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, but still.
Original Source
回答3:
The .
is the concatenation operator in PHP, for putting two strings together.
The comma can be used for multiple inputs to echo
.
回答4:
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:
void echo ( string $arg1 [, string $... ] )
Use the dot for concatenation.
回答5:
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 strings
回答6:
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 the return
statement.
来源:https://stackoverflow.com/questions/1466408/difference-between-period-and-comma-when-concatenating-with-echo-versus-return