This question already exists:
Closed 10 years ago.
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?
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 == ""
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...
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.
== checks if the value is equal e.g.:
>> "123" == 123
<< true
=== checks if the value & type are equal e.g.:
>> "123" === 123
<< false
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 9 years ago.
Does it really matter to put variables after == or === when doing comparisons?
if (null == $var) {
}
if ($var == null) {
}
I've constantly seen coders prefer this way, is it for speed difference?
Updates:
Sorry I didn't make my question clear, what I want to know is not the different between == and ===, but expressions like null == $var and $var == null, so I've changed the code example.
Conslusion:
No functional or performance difference. Some consider it a best practice or simply a coding style. BTW it has a cool name: Yoda Conditions :)
FYI: PHP error messages use this style, e.g.
PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Don't know why this question is marked duplicated to (The 3 different equals) not (What's the difference between 'false === $var' and '$var === false'?)
It prevents typos which cause very hard to debug bugs. That's in case you accidentally write = instead of ==:
if ($var == null) # This is true in case $var actually is null
if ($var = null) # This will always be true, it will assign $var the value null
Instead, switching them is safer:
if (null == $var) # True if $var is null
if (null = $var) # This will raise compiler (or parser) error, and will stop execution.
Stopping execution on a specific line with this problem will make it very easy to debug. The other way around is quite harder, since you may find yourself entering if conditions, and losing variable's values, and you won't find it easily.
I think that the following code will explain it:
echo (( 0 == NULL ) ? "0 is null" : "0 is not null") ."\n";
echo (0 === NULL) ? "0 is null" : "0 is not null" ;
it will output:
0 is null
0 is not null
The === operator checks for both value and type, but 0 is a number while NULL is of type NULL
The == operator checks only for value and zero can be casted to "false" or "null" hence the "truthy" that we get for the first line
Compare results from this to ifs.
echo "Test '1abc' == 1 - ";
if ('1abc' == 1) {
echo 'ok';
} else {
echo 'fail';
}
echo "\nTest '1abc' === 1 - ";
if ('1abc' === 1) {
echo 'ok';
} else {
echo 'fail';
}
Then read this page http://php.net/manual/en/language.types.type-juggling.php it's very interesting lecture ;)
I'm new to PHP and I found in my code, when I pass a FALSE to a function. It converted to null immediately. I've read some articles knowing that False and null are equal. But I don't know when this conversion happens.
Below is my code
function equal($expected, $actual){
if($expected == $actual) { //... }
}
function foo(){
$signal = getSignal();
equal(FALSE, $signal->good); //...
}
You will need to use triple equal signs === for equality. For example
false == null; // true
false === null; // false
0 == false; // true
0 === false; // false
Check the docs on comparison operators
No, they are NOT converted. You can var_dump($variable) at any time to see both the type and the value of $variable any time. But there are a few things which evaluates to false with the == comparison operator. So in fact false == null will evaluate to true just as false == 0 or false == "0" in PHP. This is why the comparison operator === comes into the picture - using that instead of == in above example, all will evaluate to false instead of true.
For more information, see http://php.net/manual/en/language.operators.comparison.php
This question already has answers here:
The 3 different equals
(5 answers)
Closed 9 years ago.
Does it really matter to put variables after == or === when doing comparisons?
if (null == $var) {
}
if ($var == null) {
}
I've constantly seen coders prefer this way, is it for speed difference?
Updates:
Sorry I didn't make my question clear, what I want to know is not the different between == and ===, but expressions like null == $var and $var == null, so I've changed the code example.
Conslusion:
No functional or performance difference. Some consider it a best practice or simply a coding style. BTW it has a cool name: Yoda Conditions :)
FYI: PHP error messages use this style, e.g.
PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Don't know why this question is marked duplicated to (The 3 different equals) not (What's the difference between 'false === $var' and '$var === false'?)
It prevents typos which cause very hard to debug bugs. That's in case you accidentally write = instead of ==:
if ($var == null) # This is true in case $var actually is null
if ($var = null) # This will always be true, it will assign $var the value null
Instead, switching them is safer:
if (null == $var) # True if $var is null
if (null = $var) # This will raise compiler (or parser) error, and will stop execution.
Stopping execution on a specific line with this problem will make it very easy to debug. The other way around is quite harder, since you may find yourself entering if conditions, and losing variable's values, and you won't find it easily.
I think that the following code will explain it:
echo (( 0 == NULL ) ? "0 is null" : "0 is not null") ."\n";
echo (0 === NULL) ? "0 is null" : "0 is not null" ;
it will output:
0 is null
0 is not null
The === operator checks for both value and type, but 0 is a number while NULL is of type NULL
The == operator checks only for value and zero can be casted to "false" or "null" hence the "truthy" that we get for the first line
Compare results from this to ifs.
echo "Test '1abc' == 1 - ";
if ('1abc' == 1) {
echo 'ok';
} else {
echo 'fail';
}
echo "\nTest '1abc' === 1 - ";
if ('1abc' === 1) {
echo 'ok';
} else {
echo 'fail';
}
Then read this page http://php.net/manual/en/language.types.type-juggling.php it's very interesting lecture ;)
if(NULL ==0){
echo "test". NULL;//output is test
echo "<br>";
echo "test". 0;//output is test0
}
If condition say both null and 0 are equal.But why did i get this result?
You have used Loose comparison == . If you use Strict comparison === then you will find the differences.
Read more :
type comparison table
NULL in PHP
Because it depends if you are looking at 0 as the number zero (as in nothing) or a string (as in the character '0').
NULL in PHP has the following properties:
NULL == NULL is true,
NULL == FALSE is true.
And in line with the relational model, NULL == TRUE fails
Over here you are comparing NULL to false whose output is true in PHP
As Nick already said: in this case, you are adding the value 0 to a string that makes it also a string. That's why you get the value test0.
Also, in your if, you are checking for zero value, not a strict true or false statement:
<?php
if( NULL == 0 ) {
echo "test" . NULL;
echo "<br>";
echo "test" . 0;
}
?>
Output:
test
test0
Now try it like this:
<?php
if( NULL === 0 ) {
echo "test" . NULL;
echo "<br>";
echo "test" . 0;
}
?>
You will see, that you get no output, because now the if statement is false.
NULL has no value, in your comparison it is evaluating to False, and 0 is also evaluated as False (so False == False, which is True), which is why the body of the loop executes.
NULL explicitly means "no value". See the documentation for what exactly NULL is.
because if you concat a string with NOTHING (null) the string will remain at it is, if you concat with the integer "0", it will be casted to string (auto-boxing) and concated to the original string...
normal behaviour?
and "null == 0" -> true, but "null === 0" -> false...
you have to check not only the VALUE (which will be "zero" for both itc), you have to check TYPE equality too with "==="
Because, a NULL string is nothing to be printed. Hence the first echo statement, concatenates:
"test" . NULL => "test" Then Nothing
While the 0 is a logical NULL, but in case of String it prints as 0.
In PHP, if you compare any value to a number, the value will be typecasted to Int or Float, and then the comparison will be done. In your case, the NULL is first typecasted to Int, which produces 0, and then compared, givng TRUE. Check PHP type comparison and type juggling.
I've always done this: if ($foo !== $bar)
But I realized that if ($foo != $bar) is correct too.
Double = still works and has always worked for me, but whenever I search PHP operators I find no information on double =, so I assume I've always have done this wrong, but it works anyway. Should I change all my !== to != just for the sake of it?
== and != do not take into account the data type of the variables you compare. So these would all return true:
'0' == 0
false == 0
NULL == false
=== and !== do take into account the data type. That means comparing a string to a boolean will never be true because they're of different types for example. These will all return false:
'0' === 0
false === 0
NULL === false
You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():
// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false); // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned
!== should match the value and data type
!= just match the value ignoring the data type
$num = '1';
$num2 = 1;
$num == $num2; // returns true
$num === $num2; // returns false because $num is a string and $num2 is an integer
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type
Please Refer to http://php.net/manual/en/language.operators.comparison.php
You can find the info here: http://www.php.net/manual/en/language.operators.comparison.php
It's scarce because it wasn't added until PHP4. What you have is fine though, if you know there may be a type difference then it's a much better comparison, since it's testing value and type in the comparison, not just value.