62

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?

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
omg
  • 136,412
  • 142
  • 288
  • 348

9 Answers9

73

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.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 12
    However echo does allow parentheses if there's only argument – Ruan Mendes Sep 09 '10 at 23:16
  • 1
    @JuanMendes it is not "echo". It's argument. Any expression can be wrapped in braces by design. $a = ("Hello world"); print_r(("hello ").("world")); etc. So it perfectly works with multiple arguments, echo (1), 2, (3); – Your Common Sense Jun 30 '22 at 13:21
  • @YourCommonSense Way to make me and another eleven people feel dumb! But you're % right; the parentheses in `echo("hello")` are useless, as if I had used `$someVar = ("test");`. I got this example from [What do parentheses around a variable do in PHP?](https://stackoverflow.com/questions/52987991/what-do-parentheses-around-a-variable-do-in-php) – Ruan Mendes Jun 30 '22 at 17:38
43

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 of a second, but still.

Original Source

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Mr.Web
  • 6,992
  • 8
  • 51
  • 86
  • 29
    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
  • 1
    For a set of small strings (sum up to 1KB, which I believe fits pretty much all cases), using concatenation is ~5% faster; with 2.5KB there is a technical tie; with 5KB the difference reverses, with concatenation ~5% slower, and certainly from there the ideal is to use comma. – David Rodrigues Jul 21 '22 at 05:22
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
  • 30,738
  • 21
  • 105
  • 131
GSto
  • 41,512
  • 37
  • 133
  • 184
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
  • 30,738
  • 21
  • 105
  • 131
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
  • 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. – Ruan 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
  • 9,408
  • 18
  • 74
  • 116
knittl
  • 246,190
  • 53
  • 318
  • 364
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
  • 30,738
  • 21
  • 105
  • 131
Kibbee
  • 65,369
  • 27
  • 142
  • 182
  • [Not a function.](http://stackoverflow.com/questions/1466408/difference-between-and-in-php#comment46879714_17437821) – Pacerier Mar 30 '15 at 13:06
4

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';
Rain
  • 3,416
  • 3
  • 24
  • 40
0

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
Nik Sou
  • 19
  • 3
0

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"
}
chillywilly
  • 405
  • 3
  • 11