I'm clearly late to the Null Safe party and trying to catch up. For some reason I can't get my head wrapped around it. I have lots of instances in my PHP code like:
if($_POST['submit'] == NULL){
**processing lots of stuff in here**
}
As of now, I understand I could use !isset($_POST['submit']) or is_null($_POST['submit']) to do this check. However, with the advent of PHP 8 and heavy reliance on Null Safe ? operators, I'm thinking such isset() and is_null() checks will be depreciated sometime in the near future.
So the question is, how would I change the old-school if($_POST['submit'] == NULL) check using a null-safe operator?
The Null-safe operator is not used in the context you’re describing. It doesn’t replace the isset()
or is_null()
functions for array elements like $_POST["some-string"]
. The best way to check if a specific key exists in an array and is not null is still isset($array["key"]);
.
Null-safe operator
The Null-safe operator in PHP (like in other object-oriented programming languages) is used to fetch properties of an object safely, without throwing an error if the object is null. It is used exclusively when dealing with objects, not arrays.
For example, imagine you have an object $car
which has a $wheel
property. The $wheel
is also an object, and it has a $diameter
property. If you want to get the wheel’s diameter, you would write it like so:
$wheelDiameter = $car->$wheel->$diameter;
So far so good. But now, imagine the $wheel
property is nullable (this means $wheel
can either be an object with a $diameter
, or it can be NULL). You don’t know which (object or NULL) it is, without checking. If it is NULL, and you try to access its $diameter
property (as above), an error would be thrown — you can’t access a property of NULL. Before the Null-safe operator, you had to do something like this to avoid such an error:
// Check the $wheel property of the $car object is not NULL
if ($car->$wheel)
{
// $wheel is not NULL - $diameter can be accessed safely.
$wheelDiameter = $car->$wheel->$diameter;
}
else
{
// $wheel is NULL - $diameter must also be NULL.
$wheelDiameter = null;
}
After the Null-safe operator, you can simplify this code by accessing the $diameter
safely:
// If $wheel is not NULL, this will access its $diameter property.
// If $wheel IS null, the chained object property reading will stop
// and $wheelDiameter will be assigned NULL.
$wheelDiameter = $car->$wheel?->$diameter;
Answer to your specific case
In your case of wanting to check if a specific value in an array is NULL, the isset()
function covers all bases. It checks if the value 1. exists, and 2. is not NULL. With this function you can combine both of these checks into one — alternatively, if you only want to check if a value is NULL, then the is_null()
function is your best choice. Neither the Null-safe operator nor anything else has replaced them functionally, and they are foundational elements of the language — it is very unlikely they will be deprecated, and if that happens it will be in a very long time.