PHP is_null() Function
Quick summary: The PHP is_null() function checks whether a variable is null.
PHP is_null() Syntax
is_null(mixed $value): bool
PHP
PHP is_null() Basic examples
$a = null;
var_dump(is_null($a));
PHP
Output:
bool(true)
Returns true if the variable is null.
var_dump(is_null(0));
PHP
Output:
bool(false)
Zero is not null.
PHP is_null() Real-world usage
$value = null;
if (is_null($value)) {
echo 'Value missing';
}
PHP
Output:
Value missing
Explicitly checks for missing values.
PHP is_null() Edge cases
var_dump(is_null(undefined_variable));
PHP
Output:
Notice: Undefined variable
The variable must exist to be checked safely.
PHP is_null() Common mistakes
Using is_null instead of strict comparison
is_null is equivalent to === null.
Incorrect
is_null($a);
Correct
$a === null;
Strict comparison is often clearer.
PHP is_null() Frequently Asked Questions
What does is_null() do?
Checks if a variable is null.
Use case?
Validating optional values.
Return type?
bool.
Common mistake?
Confusing with empty().
Alternative?
$var === null.
Strict check?
Yes.
Handles undefined?
No.
Performance?
Very fast.
Safe?
Yes.
Used in validation?
Yes.
Works with objects?
Yes.
Best practice?
Use strict comparison.