PHP string concatenation using ","?

I just discovered something.

echo $var1 , " and " , $var2;

is the same as:

echo $var1 . " and " . $var2;

What is the actual string concatenation operator in php? Should I be using . or , ?


The . operator is the concatenation operator. Your first example only works because the echo 'function' (technically it's a language construct, but lets not split hairs) accepts more than one parameter, and will print each one.

So your first example is calling echo with more than one parameter, and they are all being printed, vs. the second example where all the strings are being concatentated and that one big string is being printed.


The actual concatenation is . (period). Using , (comma) there, you are passing multiple arguments to the echo function. (Actually, echo is not a function but a PHP language construct, which means you can omit the parentheses around the argument list that are required for actual function calls.)


In the first case you just echo 3 different strings.

In the second case you concatenate the 3 strings and then echo the output.

So the answer is that, in order to concatenate strings you should use the dot (.)

链接地址: http://www.djcxy.com/p/59068.html

上一篇: jquery nestable不更新

下一篇: 使用“,”PHP字符串连接?