tags:

views:

302

answers:

6

In php, ($myvariable==0) When $myvariable is zero, the value of the expression is true; when $myvariable is null, the value of this expression is also true. How can I exclude the second case? I mean I want the expression to be true only when $myvariable is zero. Of course I can write

($myvariable!=null && $myvariable==0 )

but is there other elegant way to do this?

A: 
$myvariable===0


$a === $b

Identical TRUE if $a is equal to $b, and they are of the same type

Haim Evgi
+7  A: 
$myvariable === 0

read more about comparison operators.

SilentGhost
A: 

There's an is_null function, but this will just replace your $myvariable!=null

Soufiane Hassou
+1  A: 

The second solution wouldn't work either. The === operator is the solution to your problem.

soulmerge
A: 

Try ($myvariable === 0) which will not perform type coercion.

Skilldrick
:P guess I ain't the fastest gun in the west...
Skilldrick
A: 

Use the php function is_null( ) function along with the === operator. !== also works the way you'd expect.

Salman A