tags:

views:

74

answers:

5

I noticed recently that there are two ways to print multiple statements in PHP.

echo $a.$b; // Echo's $a and $b conjoined, and
echo $a,$b; // Echo's $a and echo's $b.

Is there any point in time where the difference between these two syntaxes matters?

+4  A: 

The difference does not matter under the vast majority of circumstances.

It might matter a little if the things you're concatenating together are exceptionally large, where the concatenation might be a little slower, and may take up more memory. However, focusing on micro-optimization is generally the worst way to speed things up.

Charles
+6  A: 

Realistically, no.

echo $a.$b first concatenates $a and $b into a new string, then passes it as a parameter to echo, which prints it out.

echo $a,$b gives two parameters to echo, which will print both out.

The latter is slightly more efficient. Not in any way that you would normally notice though.

There is a difference in how it is evaluated. echo $a, $b is like writing echo $a; echo $b;, two separate calls. $b will be evaluated after $a is echo'd. This can make a difference if your arguments are function calls which themselves echo something, but again, in practice this should be irrelevant, since it's bad practice.

deceze
+1  A: 

programmatically:

  1. concatenate 2 strings into 1 variable and send the string representation to the output buffer.

  2. looping over 2 variables and sending the string representation to the output buffer.

two different methods to produce similar results. If you multiplied these implementations by a significant number you would see some differences.

ebt
A: 

I asked a member of PHP core years ago and they said no. It's just another alternative syntax that allows you to have more than one way to do things. Under the hood, there may be some advantages, but nothing a human would notice.

Jason McCreary
+6  A: 

There is a difference that is good to note. With the second synthax, the first parameter will still output even if the second one causes an error.

<?php
echo 1, error(); // This outputs 1, then it displays an error 
?>

While the first one won't echo the first part.

<?php
echo '1' . error(); // Only displays an error
?>

When you separate your parameter with a comma, it will echo them one after the other. When you use the dot operator, it will concatenate them into a string and then it will echo it.

HoLyVieR
Interesting. Although it has very little implication in practice, since I would expect these kinds of PHP level errors to behave mostly undefined anyway. I.e. you never want to have this kind of code in your application to begin with, so whether `1` will print or not in this situation is largely irrelevant. :)
deceze
Also, the explanation goes more like this: Passing `1` and the result of `error()` as separate parameters to `echo` works, it's just that the result of `error()` doesn't exist, and PHP separately throws a warning. But at least `1` is a valid parameter. OTOH, trying to concatenate a string with the result of a non-existing function invalidates the whole expression, so nothing is passed to `echo`, and PHP will throw a warning separately anyway. **The warning itself is never passed to `echo`!**
deceze
This is exactly what I was looking for. That and the other one that mentions CONCAT'ing strings might take up more memory and the likes.Interesting Information, thank you.
Navarr
Its also interesting to note then that. function moo() { echo "Moo "; } echo "A Cow Says ".moo(); // "Moo A Cow Says" echo "A Cow Says ",moo(); // "A Cow Says Moo"
Navarr
@Navarr Good point, incorporated that into my answer. :)
deceze