jsuissa
jsuissa

Reputation: 1762

Issue where is_null not working even when conditions are false

In the code below when $start_limit and $end_limit are FALSE then A should be run. Instead B is occurring. I've tested that both variables are FALSE with var_dump.

I am using is_null because $start_limit is occasionally set to 0 and I want a condition where 0 counts as TRUE.

if (is_null($start_limit) && is_null($end_limit)) { 
         A      

} else {
         B          

}

Any suggestions as to how to get A to run when both variables are FALSE would be very much appreciated .

Upvotes: 0

Views: 72

Answers (2)

Matt Ball
Matt Ball

Reputation: 359956

Just use coercion-to-boolean. !0 and !false both evaluate to true.

if (!$start_limit && !$end_limit) { 
    // A
} else {
    // B          
}

http://ideone.com/aBbWJ

Upvotes: 1

Devin Ceartas
Devin Ceartas

Reputation: 4829

I think you want to use === instead of is_null. False is not null, but 0 !== false. The triple equality check is the type of exact matching you are looking for.

perhaps

if( false === $start_limit && !$end_limit ) {
 // there are no limits, set the course for the heart of the sun!
}
else {
  B
} 

Upvotes: 0

Related Questions