sum - PHP is confused when adding and concatenating -
i have following code:
<?php $a = 1; $b = 2; echo "sum: " . $a + $b; echo "sum: " . ($a + $b); ?> when execute code get:
2 sum: 3 why fail print string "sum:" in first echo? seems fine when addition enclosed in parentheses.
is weird behaviour anywhere documented?
both operators addition + operator , concatenation . operator have same operator precedence, since left associative evaluated following:
echo (("sum:" . $a) + $b); echo ("sum:" . ($a + $b)); so first line concatenation first , ends with:
"sum: 1" + 2 (now since numeric context string gets converted integer , end 0 + 2, gives result 2.)
Comments
Post a Comment