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

Viewed 32228

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?

9 Answers

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

In contrast with Mr.Web's answer PHP in 2022 is faster with the dots.

$ cat test.php 
<?php

$iterations = 10000;
$file_out ="./benchmark";
$precision = 8;

function dot()
{
        echo "Hello" . "World\n";
}

function comma()
{
        echo "Hello" , "World\n";
}

$begin = hrtime(true);
for ( $i=0 ; $i<$iterations; $i++) {
        dot();
}
$end = hrtime(true);
$out = "   dot: " . round($end-$begin, $precision)."\n";
file_put_contents($file_out, $out, FILE_APPEND);

$begin = hrtime(true);
for ( $i=0 ; $i<$iterations; $i++) {
        comma();
}
$end = hrtime(true);
$out = " comma: " . round($end-$begin, $precision)."\n";
file_put_contents($file_out, $out, FILE_APPEND);

$ php test.php
...snip...

$ cat benchmark 
   dot: 22893557
 comma: 29056406

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:

$object = new stdClass();
$object->property = 'test value';

Using concatenation...

echo "<pre>" . var_dump($object) . "</pre>";

... you get no line breaks, making a large object difficult to read:

object(stdClass)#1 (1) { ["property"]=> string(10) "test value" }

But using commas...

echo "<pre>" , var_dump($object) , "</pre>";

... you get line breaks for easy reading:

object(stdClass)#1 (1) {
  ["property"]=>
  string(10) "test value"
}
Related