PHP considers null is equal to zero

PhpNull

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

$myvariable === 0

read more about comparison operators.

Solution 2 - Php

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.

Solution 3 - Php

To identify as null or zero by:

  • is_int($var) if a variable is a number or a numeric string. To identify Zero, use is_numeric($var) is also the solution or use $var === 0

  • is_null($var) if a variable is NULL

Solution 4 - Php

Try ($myvariable === 0) which will not perform type coercion.

Solution 5 - Php

Use the php function is_null( ) function along with the === operator. !== also works the way you'd expect.

Solution 6 - Php

The second solution wouldn't work either. The === operator is the solution to your problem.

Solution 7 - Php

If your zero could be a string, you should also considere checking the "zero string"

($myvariable === 0 || $myvariable === '0')

Solution 8 - Php

I hade faced a similar issue in one of my projects, with a minor difference that I was also using the values ZERO as a valid value for my condition. here's how I solved it using simple logic to separate NULL from zero and other values.

    if (gettype($company_id) === 'NULL') {
        $company = Company::where('id', Auth::user()->company_id)->first();
    } else {
        $company = Company::where('id', $company_id)->first();
    }

Solution 9 - Php

$myvariable===0


$a === $b 

Identical TRUE if $a is equal to $b, and they are of the same type

Solution 10 - Php

There's an is_null function, but this will just replace your $myvariable!=null

Solution 11 - Php

For my case i found this soulution and it works for me :

if ($myvariable === NULL) {
    codehere...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStevenView Question on Stackoverflow
Solution 1 - PhpSilentGhostView Answer on Stackoverflow
Solution 2 - PhpRichardView Answer on Stackoverflow
Solution 3 - PhpPao ImView Answer on Stackoverflow
Solution 4 - PhpSkilldrickView Answer on Stackoverflow
Solution 5 - PhpSalman AView Answer on Stackoverflow
Solution 6 - PhpsoulmergeView Answer on Stackoverflow
Solution 7 - PhpThermechView Answer on Stackoverflow
Solution 8 - PhpAbhishek DeshpandeView Answer on Stackoverflow
Solution 9 - PhpHaim EvgiView Answer on Stackoverflow
Solution 10 - PhpSoufiane HassouView Answer on Stackoverflow
Solution 11 - Phpaymen messaoudiView Answer on Stackoverflow