Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between is_null "== NULL" and "=== NULL" in PHP [duplicate]

Possible Duplicate:
php == vs === operator

i have the following code fragment and it doesn't make sense to me why would NULL be evaluated in 3 different ways. Consider the variable $uploaded_filenames_array as UNKNOWN - we don't know whether it's still an array or a NULL. That's what we are trying to check.

//-----------------------------------------------
if (is_null($uploaded_filenames_array)){
    echo "is_null";
}
else{
    echo "is_NOT_null";
}
//-----------------------------------------------
if ($uploaded_filenames_array == NULL){
    echo "NULL stuff";
}
else{
    echo "not NULL stuff";
}
//-----------------------------------------------
if ($uploaded_filenames_array === NULL){
    echo "NULL identity";
}
else{
    echo "not NULL identity";
}
//-----------------------------------------------

i am getting the following response:

is_NOT_null 
NULL stuff 
not NULL identity 

can somebody help to understand what is the programmatic difference between these 3 ways of checking NULL?

like image 488
Rakib Avatar asked Aug 28 '12 09:08

Rakib


People also ask

What is the difference between NULL and NULL in PHP?

It is an identical comparison operator and it returns true if the value of $x is equal to NULL. Null is a special data type in PHP which can have only one value that is NULL. A variable of data type NULL is a variable that has no value assigned to it.

Is empty and NULL same in PHP?

NULL and empty - PHP TutorialNull is a fancy term for nothing, for not having a value. It's not zero, it's not an empty string, it's the actual lack of a value. I mean, if we can set a value into a variable, then we also have to have some way to talk about the fact that variable might not have a value at all.

Is NULL falsey in PHP?

NULL essentially means a variable has no value assigned to it; false is a valid Boolean value, 0 is a valid integer value, and PHP has some fairly ugly conversions between 0 , "0" , "" , and false . Show activity on this post. Null is nothing, False is a bit, and 0 is (probably) 32 bits.


2 Answers

is_null($a) is same as $a === null.

($a === null is bit faster than is_null($a) for saving one function call, but it doesn't matter, just choose the style you like.)

For the difference of === and ==, read PHP type comparison tables

$a === null be true only if $a is null.

But for ==, the below also returns true.

null == false
null == 0
null == array()
null == ""
like image 200
xdazz Avatar answered Sep 28 '22 00:09

xdazz


You should read this http://php.net/manual/en/language.operators.comparison.php. Also no need to use is_null function to check only on NULL. === is faster...

like image 38
Igor Timoshenko Avatar answered Sep 27 '22 23:09

Igor Timoshenko