Difference between period and comma when concatenating with echo versus return?

PhpSyntaxCompareConcatenation

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

return only allows one 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 - 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:

> php > echo $str1, $str2, $str3; > > > About 2.08 seconds > > php > 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 of a second, but still.

Original Source

Solution 3 - Php

The . is the concatenation operator in PHP, for putting two strings together.

The comma can be used for multiple inputs to echo.

Solution 4 - Php

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 - Php

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

Solution 6 - Php

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.

Solution 7 - Php

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 is this important?

Well, talk is cheap let me show you the code ( from PHP documentation)

$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

> Deprecated: The behavior of unparenthesized expressions containing > both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher > precedence

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

echo ' Here\'s ' . $name ?? 'Johnny';

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

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.

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';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionomgView Question on Stackoverflow
Solution 1 - PhpGumboView Answer on Stackoverflow
Solution 2 - PhpMr.WebView Answer on Stackoverflow
Solution 3 - PhpGStoView Answer on Stackoverflow
Solution 4 - PhpPatrick DesjardinsView Answer on Stackoverflow
Solution 5 - PhpknittlView Answer on Stackoverflow
Solution 6 - PhpKibbeeView Answer on Stackoverflow
Solution 7 - PhpRainView Answer on Stackoverflow