Skip to content
Advertisement

PHP checking for NULL value

Just out of curiosity (and a bit of necessity):

if(! is_null($var)){
     //do something
}

Is the above statement the same as

if($var != NULL){
//do something
}

Advertisement

Answer

No they are not the same.

The is_null function compairs the type also.

Example:

var_dump(is_null(0)); // bool(false) 
var_dump(0 == NULL);  // bool(true) 
var_dump(0 === NULL); // bool(false)

So in your case

if(! is_null($var)){
     //do something
}

Would be the same as

if($var !== NULL){
    //do something
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement