CakeFest 2025 Madrid: The Official CakePHP Conference

Voting

: max(four, seven)?
(Example: nine)

The Note You're Voting On

ai dot unstmann at combase dot de
17 years ago
For what I realized is that is_null($var) returns exactly the opposite of isset($var) , except that is_null($var) throws a notice if $var hasn't been set yet.

the following will prove that:

<?php

$quirks
= array(null, true, false, 0, 1, '', "\0", "unset");

foreach(
$quirks as $var) {
if (
$var === "unset") unset($var);

echo
is_null($var) ? 1 : 0;
echo isset(
$var) ? 1 : 0;
echo
"\n";
}

?>

this will print out something like:

10 // null
01 // true
01 // false
01 // 0
01 // 1
01 // ''
01 // "\0"
Notice: Undefined variable: var in /srv/www/htdocs/sandbox/null/nulltest.php on line 8
10 // (unset)

For the major quirky types/values is_null($var) obviously always returns the opposite of isset($var), and the notice clearly points out the faulty line with the is_null() statement. You might want to examine the return value of those functions in detail, but since both are specified to return boolean types there should be no doubt.

A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function.
isset() on the other hand is supposed to check for a VARIABLE's existence, which makes it a language construct rather than a function. Its sole porpuse lies in that checking. Passing anything else will result in an error.

Knowing that, allows us to draw the following unlikely conclusion:

isset() as a language construct is way faster, more reliable and powerful than is_null() and should be prefered over is_null(), except for when you're directly passing a function's result, which is considered bad programming practice anyways.

<< Back to user notes page

To Top