Reputation: 3871
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
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
Should be -
if( ($c > $b) && ($c> $a)) {
Upvotes: 1
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
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
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