Concatenate - comma vs punctuation

Greetings to all,

I am an old guy who have started to learn html, css and php.
Is there any difference using comma vs punctuation when concatenate?
For example:

[php]
$number = 5;
$text = “hello”;

echo “Coool string”, " and another string" . " " . $number, 5 * 44;
[/php]

Both seems to work but all books I have started to read all use punctuation…but why not use comma?

I have honestly never seen a comma used this way and I’m surprised it doesn’t issue any warnings.

It’s not even listed as a string operator:

http://www.php.net/manual/en/language.operators.string.php

You should certainly stick with the period. Here is a good link to learn about strings and the various ways to build them:

http://php.net/manual/en/language.types.string.php

I also recommend everyone develop a coding standard. I personally use the PEAR standards:

http://pear.php.net/manual/en/standards.php

Ah, this does seem to be only related to the echo function so it should never be used IMO.

For example:

[php]
$output = “Coool string”, " and another string" . " " , $number, 5 * 44;
echo $output;
[/php]

This issues “Parse error: syntax error, unexpected ‘,’”

Just for further clarification…

echo doesn’t do any concatenation, so take this for example:

[php]
$a = "This is a " . “concatenated” . “string”;
$b = "This is a " . “concatenated” . “string”;
$c = "This is a " . “concatenated” . “string”;

echo $a;
echo $b;
echo $c;
[/php]

You could instead do

[php]
echo $a, $b, $c;
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service