0
Why it giving answer 20?
<?php $str = "s10"; $int = 20; $sum = $str + $int; echo ($sum); ?>
2 Antworten
+ 4
Similar to type coercion:
http://codelegance.com/embracing-coercion-in-php/
Probably type juggling:
http://php.net/manual/en/language.types.type-juggling.php
PHP changes types of variables according to the context in which they're used.
+ is a numeric arithmetic context (ask: how does PHP coerce ... or juggle ... a string into a number)?
+ 3
s10 is an string so you cant add string and int so it give answer as 20 if you write 10s then it will give the sum as 30 because
It casts '10s' as a number, getting $str = 10.
When you echo, you add 20, to $sum, so it prints 30and $sum = 30.