0

Why it giving answer 20?

<?php $str = "s10"; $int = 20; $sum = $str + $int; echo ($sum); ?>

25th Dec 2018, 6:04 PM
RONGALA LOVARAJU
RONGALA LOVARAJU - avatar
2 ответов
+ 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)?
25th Dec 2018, 6:40 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
25th Dec 2018, 6:46 PM
MsJ
MsJ - avatar