1

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 of 1 or true. Comparing that boolean value to the integer value 1 (i.e., bool(1) > $v) will result in NULL, so the output will be “no”.

Why does bool(1) > $v become null and not undefined or false?

MC Emperor
  • 20,870
  • 14
  • 76
  • 119
Etep
  • 1,217
  • 3
  • 15
  • 25

2 Answers2

7

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. ;)

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
1

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
verhie
  • 1,169
  • 6
  • 5