[ACCEPTED]-Difference between period and comma when concatenating with echo versus return?-language-construct
return
does only allow one single expression. But 5 echo
allows a list of expressions where each 4 expression is separated by a comma. But 3 note that since echo
is not a function but a 2 special language construct, wrapping the 1 expression list in parenthesis is illegal.
You also have to note that echo
as a construct 9 is faster with commas than it is with dots.
So if you join 8 a character 4 million times this is what 7 you get:
echo $str1, $str2, $str3;
About 2.08 seconds
echo 6 $str1 . $str2 . $str3;
About 3.48 seconds
It almost 5 takes half the time as you can see above.
This 4 is because PHP with dots joins the string 3 first and then outputs them, while with commas 2 just prints them out one after the other.
We 1 are talking about fractions, but still.
The .
is the concatenation operator in PHP, for 2 putting two strings together.
The comma can 1 be used for multiple inputs to echo
.
Dot (.
) is for concatenation of a variable 8 or string. This is why it works when you 7 echo while concatenating two strings, and 6 it works when you return a concatenation 5 of a string in a method. But the comma doesn't 4 concatenate and this is why the return statement 3 won't work.
echo
is a language construct that 2 can take multiple expressions which is why 1 the comma works:
void echo ( string $arg1 [, string $... ] )
Use the dot for concatenation.
echo
is a language construct (not a function) and 5 can take multiple arguments, that's why 4 ,
works. using comma will be slightly even 3 (but only some nanoseconds, nothing to worry 2 about)
.
is the concatenation operator (the 1 glue) for strings
echo
is actually a function (not really, but 9 let's say it is for the sake of argument) that 8 takes any number of parameters and will 7 concatenate them together.
While return
is not 6 a function, but rather a keyword, that tells 5 the function to return the value, and it 4 is trying to interpret ,
as some kind of 3 operator. You should be using .
as the concatenation 2 operator in the case when you are using 1 the return
statement.
It's worth mentioning that the concatenation 23 operator .
has a higher precedence than lots 22 of other operators and has equal precedence 21 with +
and -
operators
Why this is important?
Well, talk is cheap 20 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 19 first line will issue a deprecation message 18 as of PHP 7.4.0
Deprecated: The behavior 17 of unparenthesized expressions containing both 16 '.' and '+'/'-' will change in PHP 8: '+'/'-' will 15 take a higher precedence
So in PHP 8 it 14 seems the problem of associativity in this 13 case will be solved by giving +
and -
operators 12 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 11 for example
echo ' Here\'s ' . $name ?? 'Johnny';
Here we used the Null coalescing operator so if $name 10 exists and is not NULL it'll be returned 9 otherwise it returns Johnny. At first glance, one 8 may think the result will be Here's Johnny since $name 7 is not defined or so they hope.
Actually 6 the result will be
PHP Notice: Undefined variable: name
Here's
What happened here is 5 that ??
operator has a lower precedence than 4 the .
which means PHP will try to evaluate 3 (Here's $name) first.
You can solve this by either enclosing 2 the expression in parentheses
echo ' Here\'s ' . ($name ?? 'Johnny');
Or simply use 1 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
0
There is another interesting difference. Per 8 @phirschybar's answer to this question, if you use commas 7 in conjunction with html <pre>
you get a result 6 that maintains line breaks. This is useful 5 for viewing a formatted object. For example, given 4 this object:
$object = new stdClass();
$object->property = 'test value';
Using concatenation...
echo "<pre>" . var_dump($object) . "</pre>";
... you 3 get no line breaks, making a large object 2 difficult to read:
object(stdClass)#1 (1) { ["property"]=> string(10) "test value" }
But using commas...
echo "<pre>" , var_dump($object) , "</pre>";
... you 1 get line breaks for easy reading:
object(stdClass)#1 (1) {
["property"]=>
string(10) "test value"
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.