This is due to php operator precedence ...
An expression containing .
, is executed before expressions containing +
, so the implicit parentheses are as follows:
.+-
are equal operators (they have no priority), and they are executed sequentially from beginning to end, so the implicit brackets are as follows:
echo ("$a+$b=". $a)+$b
So, if you want to get the correct result, you should use:
echo "$a+$b=". ($a+$b)
Empirical examples:
php > echo "foo" . 1 + 2; // 2 php > echo "foo" . (1 + 2); // foo3 php > echo 1 + 3 . 'foo'; // 4foo
And why it works ... Since coma separates the arguments of the function, and php sees this as:
echo( "$a+$b=", $a+$b);
And the coma ( ,
) operator is evaluated as the last.
source share