Difference between period and comma when concatena

2019-01-04 01:52发布

I just found that this will work:

echo $value , " contiue";

but this does not:

return $value , " contiue";

While "." Works in both.

What is the difference between a period and a comma here?

6条回答
【Aperson】
2楼-- · 2019-01-04 02:22

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

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.

Source

查看更多
Lonely孤独者°
3楼-- · 2019-01-04 02:27

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

查看更多
做个烂人
4楼-- · 2019-01-04 02:29

Dot (.) is for concatenation of a variable or string. This is why it works when you echo while concatenating two string 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

查看更多
老娘就宠你
5楼-- · 2019-01-04 02:33

the . is the concatenation operator in PHP, for putting two strings together. The comma can be used for multiple inputs to echo.

查看更多
看我几分像从前
6楼-- · 2019-01-04 02:45

echo is actually a function (not really but let's say it is for 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.

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-04 02:46

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.

查看更多
登录 后发表回答