Daniel Jørgensen
Daniel Jørgensen

Reputation: 1202

PHP - Switch case evaluates to true while the same statement in if doesnt

I don't quite understands this.. Take a look at the following:

$value = 0;
if($value >= 90) {
    // this does not return true
}
switch($value) {        
    case $value >= 90:              
        // this however does
    break;  
}

Am i missing something very obvious ?

Upvotes: 1

Views: 508

Answers (1)

u_mulder
u_mulder

Reputation: 54841

$value >= 90 evaluates to false

As $value is 0, it is considered false. That's why your case works.

In a simple way it can be rewritten as:

switch($value) {        
    case false:              
        // this works
    break;  
}

Upvotes: 5

Related Questions