Adding string with number in php
It casts '3dollars' as a number, getting $a = 3
.
When you echo, you add 20, to $a
, so it prints 23
and $a = 23
.
Then, when you print, you again add 20, so now $a = 43
.
The right way to add (which is technically concatenating) strings is
$a = 7;
$b = "3 dollars";
print ($a . $b); // 73 dollars
The +
operator in php automatically converts string into numbers, which explains why your code carried out arimethic instead of concatenation