All the ways to handle null values in PHP
Null is a special data type in PHP that represents a variable with no value. A variable is considered to be null if:
- it has been assigned the constant
null
. - it has been set to
null
using thesettype()
function.
In this article, we’ll go through all the ways you can handle null values in PHP.
- The is_null() function
- The null coalescing operator
- The null coalescing assignment operator
- The nullsafe operator
- The ternary operator
- Nullable types
- Null as a standalone type
The is_null() function
The is_null()
function is used to check whether a variable is null or not. It returns true
if the variable is null and false
otherwise.
$foo = null;
if (is_null($foo)) {
echo '$foo is null';
}
// $foo is null
The is_null()
function can not check whether a variable is set or not. So, you need to use the isset()
function to check whether a variable is set or not before using the is_null()
function.
The null coalescing operator
The null coalescing operator (??
) is used to check whether a variable is null or not. It returns the value of the variable if it’s not null and returns the value of the second operand if the variable is null.
The operator was introduced in PHP 7.0.
$foo = null;
echo $foo ?? 'bar';
// bar
You can also chain the null coalescing operator to check multiple variables.
$foo = null;
echo $foo ?? $bar ?? 'baz';
// baz
The one gotcha here is that the null coalescing operator will return the value of the second operand even if the variable is not set or it’s undefined.
echo $bar ?? 'foo'; // foo
The null coalescing assignment operator
The null coalescing assignment operator (??=
) is used to assign a value to a variable if it’s null. It assigns the value of the right-hand operand to the left-hand operand if the left-hand operand is null.
$foo = null;
$foo ??= 'bar';
echo $foo;
// bar
The nullsafe operator
The nullsafe operator (?->
) is used to safely call a method/property on a variable if it’s not null. It returns the value of the method if the variable is not null and returns null otherwise.
The operator is introduced back in PHP 8.0.
class User
{
public function getName()
{
return 'John Doe';
}
}
$user = null;
echo $user?->getName();
// null
$user = new User;
echo $user?->getName();
// John Doe
The ternary operator
You can still use the good old ternary operator to check whether a variable is null or not. But it’s not recommended to use it since it’s verbose and not as readable as the null coalescing operator.
$foo = null;
echo $foo ? $foo : 'bar';
// bar
Nullable types
Sometimes, you might want to explicitly declare a function parameter or a return type to be nullable. You can do so by using the ?
operator before the type declaration.
function foo(?string $bar): ?string
{
return $bar;
}
echo foo(null);
// null
echo foo('bar');
// bar
This way, you can explicitly tell the function that the parameter $bar
can be null and the function can return null as well.
Nullable types have existed since PHP 7.1.
Null as a standalone type
From PHP 8.2 onwards, you can use null
as a standalone type.
This means you can declare a variable to be of type null
and it will only accept null
as a value.
Similarly, you can also set the return type of a function to be null
and it will only return null
as a value.
class Nil
{
public null $nil = null;
public function isNull(null $nil): null
{
return null;
}
}
$nil = new Nil;
$nil->isNull(null);
// returns null
$nil->isNull('foo');
// PHP Fatal error: Uncaught TypeError: Nil::isNull():
// Argument #1 ($nil) must be of type null, string given
One thing to keep in mind when type-hinting method and variables using null
is, that if you try to mark null as nullable (?null
), it will result in a compile-time error which is in line with PHP’s current type resolving redundancy rules.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.