Skip to lesson
Back to Kodivo

Values and Types PHP

Null and missing values

Null has one value

The null type has exactly one value: null. The constant is case-insensitive, so NULL denotes the same value. An undefined or unset variable resolves to null, but reading an undefined variable directly also issues a warning.

PHP
<?php
$value = null;
var_dump($value, $value === false, $value === 0);

Output

NULL
bool(false)
bool(false)
  • null is distinct from false, 0, and the empty string.
  • Assigning null and removing a variable with unset are different operations.

Check whether a value is set

isset returns false when a variable is undefined or has the value null. It returns true for an existing variable holding an empty string. This excerpt retains the manual's null check.

PHP
<?php
$foo = NULL;
var_dump(isset($foo));   // FALSE

Output

bool(false)

Unsetting removes the variable

unset removes a variable; it is a language construct rather than a cast. Use is_null to check whether an existing value is null. isset alone cannot distinguish an undefined variable from a variable explicitly assigned null.

PHP
<?php
$value = null;
var_dump(is_null($value), isset($value));
unset($value);
var_dump(isset($value));

Output

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

Check your understanding

What does var_dump report?

<?php
$value = null;
var_dump(isset($value));
Choose an answer

Practice here. Create an account to save your progress.

Sources and notes

NULL

Recognize null, unset values, isset, and the distinction from false and an empty string. Selected scope only; later chapter detail follows the complete PHP program catalog.

isset

Recognize null, unset values, isset, and the distinction from false and an empty string. Selected scope only; later chapter detail follows the complete PHP program catalog.

unset

Recognize null, unset values, isset, and the distinction from false and an empty string. Selected scope only; later chapter detail follows the complete PHP program catalog.

Basics

Recognize null, unset values, isset, and the distinction from false and an empty string. Selected scope only; later chapter detail follows the complete PHP program catalog.

is_null

Recognize null, unset values, isset, and the distinction from false and an empty string. Selected scope only; later chapter detail follows the complete PHP program catalog.

Credits and license

PHP Documentation Group. CC BY 3.0 or later.

Adapted and condensed from the cited pinned PHP manual sources by Kodivo with AI assistance. Examples preserve complete source listings or explicitly identified excerpts. This is not an official PHP course. This revision adds original Kodivo worked examples and artwork prepared with AI assistance; the cited manual remains the source for the technical rules.

Swipe up to continue