0

Why output = 2

<?php $b = 0; $c =1; function get($x) { return $x; } if($a=get($b)) { echo 1; } if($a=get($c)) { echo 2; }

4th Jun 2020, 7:21 AM
Hm Hasan
Hm Hasan - avatar
3 Answers
+ 4
if($a=get($b)) This evaluates to false so the code is not run. $b equals zero and $a is set to zero via the get function, then its value is checked for the if statement. Zero is equivalent to false. Likewise, if($a=get($c)) evaluates to true. $c equals 1. $a is set to 1 via the get function and is then checked for the if statement. 1 is equivalent to true, so the code in the if statement is run outputting 2.
4th Jun 2020, 8:00 AM
ChaoticDawg
ChaoticDawg - avatar
0
yeap,,but first if statement have also echo
4th Jun 2020, 7:39 AM
Hm Hasan
Hm Hasan - avatar