+ 2

Explain this code ! (PHP)

<?php $a = "hello"; $a += ""; $a += "world"; echo $a; ?> Why the output is 0 ?

28th Apr 2018, 7:41 AM
Rhythm Khandelwal
3 odpowiedzi
+ 5
Like KrOW said, you need to use the dot operator for string concatenation in PHP. PHP is loosely typed, meaning it won't moan if you do crazy things with different types. The plus operator implies numerical addition, so that's what it will do! "hello" is equal to zero, but "5" would be equal to 5. All your strings are numerically equal to zero, according to PHP!
28th Apr 2018, 8:42 AM
Emma
+ 3
i know few of php but if my memory is good string concatenation is maded with dot "." operator $a = $a." world"; and php dont show error if debug is disabled
28th Apr 2018, 8:35 AM
KrOW
KrOW - avatar
+ 1
Use ". =" in PHP not "+=" Concatenation in PHP work with "."(dot) symbol
28th Apr 2018, 10:12 AM
Ivan
Ivan - avatar