Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP considers null is equal to zero

Tags:

php

null

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?

like image 515
Steven Avatar asked Nov 23 '09 12:11

Steven


People also ask

Is NULL the same as 0 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.

Is 0 considered empty PHP?

From manual: Returns FALSE if var has a non-empty and non-zero value. The following things are considered to be empty: "" (an empty string) 0 (0 as an integer)

What is the value of NULL in PHP?

In PHP, a variable with no value is said to be of null data type.

Does PHP support NULL?

Definition and Usage. The is_null() function checks whether a variable is NULL or not. This function returns true (1) if the variable is NULL, otherwise it returns false/nothing.


2 Answers

$myvariable === 0 

read more about comparison operators.

like image 94
SilentGhost Avatar answered Sep 22 '22 23:09

SilentGhost


You hint at a deep question: when should an expression be true?

Below, I will explain why what you are doing isn't working and how to fix it.

In many languages null, 0, and the empty string ("") all evaluate to false, this can make if statements quite succinct and intuitive, but null, 0, and "" are also all of different types. How should they be compared?

This page tells us that if we have two variables being compared, then the variables are converted as follows (exiting the table at the first match)

Type of First  Type of Second   Then null/string    string           Convert NULL to "", numerical/lexical comparison bool/null      anything         Convert to bool, FALSE < TRUE 

So you are comparing a null versus a number. Therefore, both the null and the number are converted to boolean. This page tells us that in such a conversion both null and 0 are considered FALSE.

Your expression now reads, false==false, which, of course, is true.

But not what you want.

This page provides a list of PHP's comparison operators.

Example   Name                Result $a ==  $b  Equal              TRUE if $a equals $b after type juggling. $a === $b  Identical          TRUE if $a equals $b, AND they are of the same type. $a !=  $b  Not equal          TRUE if $a not equals $b after type juggling. $a <>  $b  Not equal          TRUE if $a not equals $b after type juggling. $a !== $b  Not identical      TRUE if $a not equals $b, or they are not of the same type. $a  <  $b  Less than          TRUE if $a is strictly less than $b. $a  >  $b  Greater than       TRUE if $a is strictly greater than $b. $a <=  $b  Less than/equal    TRUE if $a is less than or equal to $b. $a >=  $b  Greater than/equal TRUE if $a is greater than or equal to $b. 

The first comparator is the comparison you are using now. Note that it performs the conversions I mentioned earlier.

Using the second comparator will fix your problem. Since a null and a number are not of the same type, the === comparison will return false, rather than performing type conversion as the == operator would.

Hope this helps.

like image 41
Richard Avatar answered Sep 24 '22 23:09

Richard