What's the Difference Between Is_Null($Var) and ($Var === Null)

What's the difference between is_null($var) and ($var === null)?

Provided the variable is initialized (which you did indicate - though I'm not 100% sure if this matters in this context or not. Both solutions might throw a warning if the variable wasn't defined), they are functionally the same. I presume === would be marginally faster though as it removes the overhead of a function call.

It really depends on how you look at your condition.

=== is for a strict data comparison. NULL has only one 'value', so this works for comparing against NULL (which is a PHP constant of the null 'value')

is_null is checking that the variable is of the NULL data type.

It's up to you which you choose, really.

difference between is_null == NULL and === NULL in PHP

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

PHP is_null() and ==null

is_null is the same as === null. Both return true when a variable is null (or unset).

Note that I'm using === and not ==. === compares type as well as value.

isset vs empty vs is_null

isset() will check if the variable is set, ie

<?php

echo isset($var); // false

$var = 'hello';

empty() will check if the variable is empty, ie

<?php

$emptyString = '';

echo empty($emptyString); // true

is_null() will check for NULL which is different from empty, because it's set to NULL not an empty string. (NULL might be a confusing concept)

Since your title is a string, I think you want to be using empty()

if (!isset($_REQUEST[$name_input_name]) || empty($_REQUEST[$name_input_name])) {
$file->error = 'Please Enter a Title';
return false;
}

is_null($x) vs $x === null in PHP

There is absolutely no difference in functionality between is_null and === null.

The only difference is that is_null is a function and thus

  1. is marginally slower (function call overhead)
  2. can be used as a callback, e.g. array_map('is_null', $array).

Personally, I use null === whenever I can, as it is more consistent with false === and true === checks.

If you want, you can check the code: is_identical_function (===) and php_is_type (is_null) do the same thing for the IS_NULL case.


The related isset() language construct checks whether the variable actually exists before doing the null check. So isset($undefinedVar) will not throw a notice.

Also note that isset() may sometimes return true even though the value is null - this is the case when it is used on an overloaded object, i.e. if the object defines an offsetExists/__isset method that returns true even if the offset is null (this is actually quite common, because people use array_key_exists in offsetExists/__isset).

PHP: Using is_null() with !$var or isset($var)

Yes, both snippets are equivalent. is_null is defined as:

Returns TRUE if var is null, FALSE otherwise.

The documentation also makes it clear that is_null throws out a warning when the variable is undefined, as does a simple boolean evaluation of $result. If $result is unset, is_null($result) is true and you therefore get one warning - the same behavior as you'd with !$result.

Since the boolean evaluation of NULL is (unsurprisingly) false, we can simply test out all interesting values:

$result  is_null($result)  !$result   is_null($result) || !$result
(unset) true(+warn) true(+warn) true (+warn)
null true true true
false(-y) false true true
true(-ish) false false false

Note that the results of is_null and !$result are identical for all values that evaluate to false as well for all ones evaluating to true. Therefore, no further distinction (say, by testing 0, "", etc.) is necessary.

What is the point of is_null()?

is_null() Finds whether the variable is NULL

You really need isset() which determines if a variable is set and is not NULL. Returns TRUE if variable exists and has value other than NULL, otherwise FALSE.

For instance,

$something = null; then isset($something) returns false
$something = 'some value'; then isset($something) returns true

How to differentiate a value is zero or NULL in php?

to check for null use :

if (is_null($moon))

to check for integer 0 use:

if ($moon === 0)

to check for integer 0 or string "0"

if ($moon == '0')

I have to add that php will consider any string equals with number 0 when using "==":

$moon = "asd";

if ($moon == 0) // will be true

php is null when empty?

What you're looking for is:

if($variable === NULL) {...}

Note the ===.

When use ==, as you did, PHP treats NULL, false, 0, the empty string, and empty arrays as equal.



Related Topics



Leave a reply



Submit