+ 3

Why the result is 0 in the script below?

https://code.sololearn.com/wXhOF1ZxuxXV/?ref=app Strasse answer but fact.

27th Jun 2019, 3:54 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
7 ответов
+ 5
idk its kinda part of php where string that doesnt have recognizeable numeric at the start will result in 0 if casted. since the operation is + i think php will treat the variable as number, so it'll cast that to number which is 0 for both of them. so 0 + 0 = 0. the result will be different if you use $crbl="7 world";
27th Jun 2019, 4:05 PM
Taste
Taste - avatar
+ 4
If you Wand to combine strings use $combined=$first . $second
27th Jun 2019, 4:23 PM
Lexfuturorum
Lexfuturorum - avatar
+ 4
OK, I expected - hello world. Thanks, all of You, espacially to You, David.
27th Jun 2019, 6:18 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 3
EGO What result were you expecting?
27th Jun 2019, 4:13 PM
David Carroll
David Carroll - avatar
+ 3
every one but not 0 -:) BEFORE these posts.
27th Jun 2019, 5:52 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 3
So, IF You write echo $crbl, it is appeared 'world' but not zero.
27th Jun 2019, 6:00 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 3
EGO PHP uses the dot (.) operator to concatenate string values. Therefore, the following line would result in "helloworld": $vrbl . $crbl However, the code is using the plus (+) operator, which will attempt an arithmetic addition on the 2 operands. If the operand is a string with a value that cannot be converted into a number, it appears that PHP will use the default integer value, which is 0. Therefore, "hello" + "world" is essentially the same as 0 + 0.
27th Jun 2019, 6:11 PM
David Carroll
David Carroll - avatar