Question

My question is regarding the use of is_null().

I've read other questions that discuss is_null($x) versus null === $x, but I am more concerned with why there is an is_null() function at all?

A few tests to explain my thinking:

<?php

header('Content-type: text/plain');
error_reporting(-1);

$test = 'Hello, World!';
$test2 = null;
$test3 = '';

var_dump(is_null($test));
var_dump(null === $test);
var_dump(isset($test));

var_dump(is_null($test2));
var_dump(null === $test2);
var_dump(isset($test2));

var_dump(is_null($test3));
var_dump(null === $test3);
var_dump(isset($test3));

var_dump(is_null($test4));
var_dump(null === $test4);
var_dump(isset($test4));

which will produce the following output:

bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)

Notice: Undefined variable: test4 in C:\home\ombrelle.co.uk\templates_core\test.php on line 22
bool(true)

Notice: Undefined variable: test4 in C:\home\ombrelle.co.uk\templates_core\test.php on line 23
bool(true)
bool(false)

As you can see, when using the is_null() function or comparison method it will throw a notice, so you're forced to use isset() instead. So the only way to never see a notice using these methods is when it's not null?

Also consider the following:

<?php

header('Content-type: text/plain');
error_reporting(-1);

var_dump((is_null($test1)) ? 'test1' : $test);
var_dump((null == $test2) ? 'test2' : $test);
var_dump((isset($test3)) ? 'test3' : $test);

giving the following output:

Notice: Undefined variable: test1 in C:\home\ombrelle.co.uk\templates_core\test.php on line 6
string(5) "test1"

Notice: Undefined variable: test2 in C:\home\ombrelle.co.uk\templates_core\test.php on line 7
string(5) "test2"

Notice: Undefined variable: test in C:\home\ombrelle.co.uk\templates_core\test.php on line 8
NULL

Here in a ternary statement, the aforementioned work, still with notices, however the isset() method now doesn't work at all. How would one go about this properly, without showing notices?

After all that, am I just to accept that notices are pointless and shouldn't be sent to my error log, or are there any other caveats I should consider?

We are currently cleaning an old system of lots of errors - we don't want to miss any, but also there's no point creating more errors for ourselves either. Any links to authoritative reading on the matter are also greatly appreciated.

Was it helpful?

Solution

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

OTHER TIPS

The only difference I can think of (except for being way slower - like the answer in the linked answer you posted), is that is_null allows use of the horrible @ operator:

<?php
    error_reporting(E_ALL);

    var_dump($foo === null); //Notice
    var_dump(@is_null($foo)); //No notice
?>

DEMO

That said, if you aren't sure if a variable exists you should use isset and not is_null.

On top of that, you might be interested in Best way to test for a variable's existence in PHP; isset() is clearly broken

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top