Difference between period and comma when concatenating with echo versus return?
Solution 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.
Solution 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.
Solution 3
The .
is the concatenation operator in PHP, for putting two strings together.
The comma can be used for multiple inputs to echo
.
Solution 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.
Solution 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
omg
Updated on October 15, 2021Comments
-
omg over 2 years
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?