PHP: Why will the code in the if block run? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

PHP: Why will the code in the if block run?

$a = 2; $b = „Hello world“; if($a && $b) { //the following code will run }

12th Jul 2019, 5:49 PM
Niklas
3 Answers
+ 1
Because everything diffrent from zero will be interpreted as true! Or any non Empty Objects or Strings/Arrays... I know this from C where it is the same.
12th Jul 2019, 6:34 PM
Checker 8763
Checker 8763 - avatar
+ 1
This may have to do with the bitwise representation of a empty string.
12th Jul 2019, 6:50 PM
Checker 8763
Checker 8763 - avatar
0
empty strings will also be interpreted as true $c = „“;
12th Jul 2019, 6:37 PM
Niklas