Saw this question on Toptal and got a little confused:
$v = 1;
$m = 2;
$l = 3;
if ($l > $m > $v) {
echo "yes";
}
else {
echo "no";
}
I get why the answer is "no", but their reasoning was a bit confusing to me:
First,
$l > $m
will be evaluated which yields a boolean value of1
ortrue
. Comparing that boolean value to the integer value 1 (i.e.,bool(1) > $v
) will result inNULL
, so the output will be “no”.
Why does bool(1) > $v
become null
and not undefined
or false
?
It doesn't. true > 1
is false
(because they are instead "equal").
Furthermore, the entire program will not run because the "double conditional" is a parse error.
The answer you read is wrong.
This is why you shouldn't take advice from random strangers on the internet. Including me. ;)
Just for the record
<?php
$v = 1;
$m = 2;
$l = 3;
var_export( $l > $m ); // true
var_export( $m > $v ); // true
var_export( $l > ($m > $v) ); // false: (3 > true) = false
var_export( ($l > $m) > $v ); // false: (true > 1) = false
var_export( $l > $m > $v ); // parse error