difference between is_null “== NULL” and “=== NULL” in PHP [duplicate]

怎甘沉沦 提交于 2019-12-06 19:27:15

问题


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?


回答1:


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 == ""



回答2:


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...




回答3:


The === operator tests for the same value and the same TYPE. An empty string might evaluate to null, but it is not of the null type - hence this fails.

The == operator basically checks to see if they are pretty much the same - by that, do the evaluate to the same value. Being empty, this will evaluate to null, hence this fails.

The is_null function does a fairly thorough check - much more like the === operator.




回答4:


== checks if the value is equal e.g.:

>> "123" == 123
<< true

=== checks if the value & type are equal e.g.:

>> "123" === 123
<< false


来源:https://stackoverflow.com/questions/12156395/difference-between-is-null-null-and-null-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!