4

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 ,?

2
  • 1
    now we do a big random to pick whose answer is right! Commented Sep 29, 2009 at 8:20
  • omg.. u r right? This question is too stupid (duh, I am the stupid question-er).. i will choose the one that has highest vote. Thanks guys(its really hard for me to choose the best answer, I just refresh this page, and I got lots of answers z_z)!! Commented Sep 29, 2009 at 8:24

8 Answers 8

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

One more note. echo does not take multiple arguments if you call it using parens. This won't work: echo($a, $b);
I read somewhere that echo "a", "b"; is faster than echo "a"."b";
I doubt it's noticably faster, except perhaps for very large 'a' and 'b'. I do believe it though.
4

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.)

Comments

3

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 (.)

Comments

2

".". The other just writes several values independently, without actually concatenating the string.
See also PHP echo reference, the "," variant will only work with methods accepting multiple parameters.

Comments

2

"." concatenates, "," can only be used for echo which is a language construct (sort of a function)

also see: Difference between period and comma?

Comments

1

Using a comma doesn't actually concatenate the strings.

See this answer to another question.

Comments

1

The "." is the correct concatenate operator. "echo" also accepts ",", treating it as if you are passing in a series of arguments to the "echo" method and then echoing each one. It's not truly concatenating the strings.

Comments

0

nop it's because echo could take more then one argument, it would do the print each arguments

2 Comments

I doubt echo does a concatentation, it just prints each parameter.
yeah but happens to have the same effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.