Ali Zia
Ali Zia

Reputation: 3871

Why is this if statement returning false every time?

I have a piece of code. It is returning false every time, despite the fact that condition is true. Where am I wrong?

$a = 5;
$b = 10;
$c = 15;

if( ($c > $b) > $a){
    echo "yes";
} else {
    echo "no";
}

Upvotes: 2

Views: 61

Answers (4)

Sougata Bose
Sougata Bose

Reputation: 31749

For your data

($c > $b) is true.

true > $a is false.

var_dump(100 < TRUE); // FALSE - same as (bool)100 < TRUE
var_dump(-10 < FALSE);// FALSE - same as (bool)-10 < FALSE

Comparison Operators

Should be -

if( ($c > $b) && ($c> $a)) {

Upvotes: 1

Flosculus
Flosculus

Reputation: 6956

The expression can be explained as:

1. true = 15 > 10
2. 1    = (int) true
3. true = 5 > 1

($l > $m) is converted to an integer representation of true, which is 1 and 1 is less than 5

Upvotes: 1

syck
syck

Reputation: 3029

($c > $b) yields true, represented by a 1 in numeric expressions.

1 > $a is definitively false.

Probably ($c>$b && $b>a) is what you are looking for, if you want be $b in the range between $c and $a.

Upvotes: 1

Andrius
Andrius

Reputation: 5939

($c > $b) returns true. Then you get true > $a which would be false.

You should either nest the if statement or think of something like:

if( $c > $b &&  $c > $a){

Upvotes: 2

Related Questions