Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 655
omg Avatar asked Sep 23 '09 14:09

omg


People also ask

What is the difference between using concatenation and using commas?

concatenation means you are combining both variables(only strings) into one. But when you use comma it prints both the output separately but you can't see it on the output console.

What does comma do in PHP?

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.

What is the difference between a comma and a period?

The use of the comma (,) is to help with shortening long sentences, or joining two sentences, or even to list a number of words within one sentence. If a long sentence leaves you out of breath when you are reading it out loud, add a comma to give a brief breathing break to the reader. The period or full-stop (.) ends a sentence.

Is it possible to echo an expression with a comma?

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. Show activity on this post. You also have to note that echo as a construct is faster with commas than it is with dots.

Why does the return statement not work with the comma?

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. Show activity on this post.

Is it faster to echo a string with commas or dots?

You also have to note that echo as a construct is faster with commas than it is with dots. 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. Show activity on this post.


2 Answers

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.

like image 97
Gumbo Avatar answered Sep 29 '22 15:09

Gumbo


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

like image 40
Mr.Web Avatar answered Sep 29 '22 15:09

Mr.Web