PHP developers! If you’ve been coding with PHP for any amount of time, there’s no way you haven’t come across null values. And if you haven’t, well, it’s only a matter of time until you do!
A null value is a special value in programming that represents the absence of any value or data. Think of it as an empty container, where there’s no value or anything inside it. A variable assigned with a null value is useful for indicating an unknown (or undefined) state.
However, null values can cause problems if you’re not careful in how you check for them, so it’s important to know what you’re doing. If you read on, I’m going to show you exactly how to check if a variable is null in PHP — and what to watch out for. Ready?
Checking for Null in PHP
The two foolproof, most reliable ways to check for a null value in PHP are using the is_null()
function and the identical (===
) operator.
Using the is_null() Function
The is_null()
function is a built-in function in PHP that checks if a variable is null. It returns true
if the variable is null and false
otherwise.
Here’s an example:
$myVar = null;
if (is_null($myVar)) {
echo '$myVar is null';
} else {
echo '$myVar is not null';
}
Let’s put it to the test.
Suppose we have four variables to test with the is_null()
function:
$a
is null$b
equals 0$c
is an empty string$d
is undefined
Here’s how the is_null()
function would evaluate these variables’ values:
<?php
$a = null;
$b = 0;
$c = "";
if(is_null($a)) {
echo "is_null: a: TRUE";
} else {
echo "is_null: a: FALSE";
}
if(is_null($b)) {
echo "\nis_null: b: TRUE";
} else {
echo "\nis_null: b: FALSE";
}
if(is_null($c)) {
echo "\nis_null: c: TRUE";
} else {
echo "\nis_null: c: FALSE";
}
if(is_null($d)) {
echo "\nis_null: d: TRUE";
} else {
echo "\nis_null: d: FALSE";
}
// OUTPUT:
// is_null: a: TRUE
// is_null: b: FALSE
// is_null: c: FALSE
// is_null: d: TRUE
// PHP Notice: Undefined variable: d in example.php on line 24
?>
Using the Identical Operator (===)
The identical operator (===
) in PHP checks if two values are equal and of the same type. If a variable is null, it will be of the type null
, so comparing it with null
using the ===
operator will return true
.
Here’s an example:
$myVar = null;
if ($myVar === null) {
echo '$myVar is null';
} else {
echo '$myVar is not null';
}
And now, to apply the same test to the identical operator (===
) as the one we applied to the is_null()
function:
<?php
$a = null;
$b = 0;
$c = "";
if($a === null) {
echo "=== null: a: TRUE";
} else {
echo "=== null: a: FALSE";
}
if($b === null) {
echo "\n=== null: b: TRUE";
} else {
echo "\n=== null: b: FALSE";
}
if($c === null) {
echo "\n=== null: c: TRUE";
} else {
echo "\n=== null: c: FALSE";
}
if($d === null) {
echo "\n=== null: d: TRUE";
} else {
echo "\n=== null: d: FALSE";
}
// OUTPUT:
// === null: a: TRUE
// === null: b: FALSE
// === null: c: FALSE
// === null: d: TRUE
// PHP Notice: Undefined variable: d in HelloWorld.php on line 24
?>
How to Choose
As you can see, both the is_null()
function and the identical operator (===
) evaluate null values in the same way, which means they are generally interchangeable within your code.
The ===
operator is faster than the is_null()
function because it is a language construct and doesn’t involve calling a function. The ===
operator is also optimized by the PHP interpreter, so it is usually faster than any function call.
However, in most cases, the difference in speed between the two methods is negligible and unlikely to have a noticeable impact on the performance of your code. In practice, you should make the choice between using is_null()
or ===
to check for null values based on the readability and clarity of your code.
When in doubt, refer to your team’s coding convention and style guide, or have a conversation with them about the method that everyone should informally follow to have clear, consistent code.