58

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
omg
  • 123,990
  • 135
  • 275
  • 341
  • This question is way too broad, because it can have many use cases that apply to the same behavior. – i am me Oct 02 '15 at 22:15

7 Answers7

69

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.

Gumbo
  • 594,236
  • 102
  • 740
  • 814
  • Although not perfect,but nearest! – omg Sep 23 '09 at 14:52
  • 10
    However echo does allow parentheses if there's only argument – Juan Mendes Sep 09 '10 at 23:16
  • @JuanMendes, How does it follow, What's the logic or **explanation** then? – Pacerier Mar 30 '15 at 12:50
  • 2
    @Pacerier The parentheses would wrap the first expression in the list like you can wrap any expression in additional parentheses. – Gumbo Mar 30 '15 at 16:36
  • @Gumbo, Wow just realised that this works: `echo 1,2,(3),4,(5);` – Pacerier Apr 06 '15 at 01:28
  • Wrapping expression lists in parantheses is illegal in general: `array((1,2,3))` will throw an error as well. – Tgr Aug 26 '15 at 21:26
  • 1
    This can be confusing to people coming from other C-like languages since those usually have a comma operator (so that `1,2,3` is a valid expression) but PHP does not. – Tgr Aug 26 '15 at 21:27
  • 1
    `print` is a language construct just as `echo` is, but the parser specifically doesn't allow `echo` after the beginning of a statement because it doesn't make sense anywhere else (`$echo=1;` doesn't count). `print ` returns 1, so it is allowed anywhere that a value can go, even within parentheses. This can be useful for debugging a series of conditions, such as this `echo $a&&(print 'a')&&$b&&(print 'b')||$c&&(print 'c') ? 1 : 0; //ac1` where apparently $a and $c were truthy and $b was not. Here's an even more interesting mess: `echo 4,print 3,print 2; //43121`. – Chinoto Vokro Dec 17 '16 at 00:29
36

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.

Original Source

Community
  • 1
  • 1
Mr.Web
  • 5,891
  • 8
  • 37
  • 75
  • 23
    i like your explanation. Its weird Ive been coding PHP for years and never knew you could comma seperate. Ive always used dots. – azzy81 Jul 17 '13 at 15:39
  • @SubstanceD :D Great! – Mr.Web Jul 17 '13 at 15:49
  • The source, so if someone decides to read further into this http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/ – Brian Leishman Jan 12 '15 at 20:02
  • @Mr.Web, Stop saying it's a function and Start the chant now people: [The echo is **not** a function. The echo is **not** a function. The echo is **not** a function. The echo is **not** a function.](http://stackoverflow.com/questions/1466408/difference-between-and-in-php#comment46879572_1466429) – Pacerier Mar 30 '15 at 13:04
  • @Pacerier ok ok! Soooorrryyyy. I fixed it, my bad. – Mr.Web Dec 08 '15 at 20:14
  • `ob_start(function(){},4000); $past=microtime(true); echo eval('echo '.trim(str_repeat('"123".', 1e5), '.').';'); $time=microtime(true)-$past; ob_end_clean(); echo $time;` If I run this, I get a segmentation fault, but if I change to 1e4 repetitions or change the dot to a comma (in the first argument of str_repeat and second of trim), it works. Conclusion: Don't concatenate if it is unnecessary, use commas instead. The empty output buffer prevents output and limits memory usage. Commas are 7x faster in this obscene benchmark (what madman concatenates 10000 values?!). – Chinoto Vokro Dec 17 '16 at 01:27
  • This could matter if there are a lot more than 3 items (quadratic behaviour is very often seen in practise - and adding a lot of small strings with full string copy for each and every small string has a name: *[Shlemiel the Painter's Algorithm](http://www.joelonsoftware.com/articles/fog0000000319.html)*) – Peter Mortensen Nov 11 '19 at 19:29
  • So is the expectation that echo $str1; echo $str2; echo $str3; will also be around 2.08 seconds in the above setup? – user3445853 Nov 28 '19 at 12:54
20

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

The comma can be used for multiple inputs to echo.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
GSto
  • 38,666
  • 37
  • 126
  • 179
11

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Patrick Desjardins
  • 125,683
  • 80
  • 286
  • 335
  • 1
    But I'm using echo 'something',not echo('something') ,say,without brackets. – omg Sep 23 '09 at 14:44
  • PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. – Patrick Desjardins Sep 23 '09 at 14:51
  • 1
    that's because echo is a keyword in PHP, in addition to being a function. you could write it as echo('something','something else') and it would also work fine. – GSto Sep 23 '09 at 14:52
  • 1
    Shore you should check the php.net website about echo. GSto and I are telling you exaclty what is written in the PHP documentation. And it works. – Patrick Desjardins Sep 23 '09 at 15:13
  • 1
    echo('something','something else') ; exit(); Parse error: syntax error, unexpected ',' – omg Sep 23 '09 at 15:16
  • 1
    you cannot use parentheses when passing multiple parameters. Nobody uses it with parentheses anyway. – Juan Mendes Sep 09 '10 at 23:18
  • 1
    `The echo is a function` - no it's not. See the answer below by @knittl – scrowler Jun 18 '14 at 22:23
  • @PatrickDesjardins, The `echo` is **not** a function. The `echo` is **not** a function. The `echo` is **not** a function. The `echo` is **not** a function. Read the docs, yes http://php.net/echo. *"echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses."*. And why do you say it works when **all** of us are getting "PHP Notice: Trying to get property of non-object in file.php on line 2"? – Pacerier Mar 30 '15 at 13:00
7

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

Gottlieb Notschnabel
  • 8,768
  • 17
  • 66
  • 107
knittl
  • 197,664
  • 43
  • 269
  • 318
5

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kibbee
  • 62,900
  • 26
  • 139
  • 178
  • [Not a function.](http://stackoverflow.com/questions/1466408/difference-between-and-in-php#comment46879714_17437821) – Pacerier Mar 30 '15 at 13:06
1

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 this is 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';
Rain
  • 1,745
  • 1
  • 11
  • 23