2

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.

Community
  • 1
  • 1
LeonardChallis
  • 7,728
  • 5
  • 42
  • 72
  • It seems that you do not distinguish variable which has `null` value and variable that is not defined. These are to different things. – Jakub Matczak Mar 03 '14 at 10:50
  • 1
    In other words: is_null is a function, just like is_bool, is_int, is_. Only since the null-type has only 1 possible value, it is in practice equal to === null. It's just like is_bool is equal to ($var === true or $var === false). A non-existant variable in a comparison is treated as a variable with no value (ie. null) and hence returns true on null === $undefined. However, when passing no-variable as an argument to a function, the function recieves nothing, while it expects at least something, and thus returns a notice. – Tularis Mar 03 '14 at 10:51
  • I think what confused me here was that `$something = null; var_dump(isset($something));` will output `false`. It's not *actually* throwing an error when using `is_null` though, if it's set to `null`. Weird. Or am I being silly? – LeonardChallis Mar 03 '14 at 11:05

2 Answers2

2

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
Linga
  • 9,691
  • 9
  • 45
  • 91
  • As I mentioned in reply to @Tularis above, when I put `$something = null; var_dump(isset($something));`, it will return `false`. So unless I **knew** that it was really set (i.e. not at code time) I couldn't use `is_null`, right? – LeonardChallis Mar 03 '14 at 11:07
0

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

Community
  • 1
  • 1
h2ooooooo
  • 36,580
  • 8
  • 61
  • 97
  • 3
    `is_null` is a way to check the `type` of the variable. `=== null` checks the `type` and the `value`. Since a `null type` has only a single possible value, those two are in practice the same. However, is_null exists to keep consistency in the language (ie. as a parallel to `is_bool`, `is_int`, `is_object`, `is_array`, etc.) – Tularis Mar 03 '14 at 10:52