I’m not an actual programmer so not writing any code as for my daily job, but at past I have written quite many rows of PHP. Lately I have worked with one older website update from PHP7.x to PHP8.x and oh boy how many problems the new more strict NULL value handling logic caused. Or let’s say that it wouldn’t be a problem for PHP8, but seems that when PHP9 comes, it would have been a major issue.
Nevertheless, I decided that I will fix the issues now, little by little so that this problem is not carried any further. After making a huge amount of corrections with isset()
function and if/else statements to handle NULL values, I started to think that what if I would write a function ensure_value($_GET['something'], 'default value')
that would ensure I always would have a value what PHP8 expects, not NULL.
But then I started to do a bit googling and almost gave up, until I hit to this very-very-good and easy to read article:
Ternary Operator in PHP | How to use the PHP Ternary Operator | Codementor
Like you can see, it has been posted about 5 years ago and I truly hope I would have seen&learnt this back then. 😀 But as I am not a programmer I haven’t kept myself that up to date with PHP and haven’t had this issue with such a magnitude before. So this was a new great learning to have now.
For me the last example of that article is the most valuable one, so in case someone seeks a solution for easy NULL value handling, this is much better than writing your own ensure_value()
function:
$something= $_GET[‘something’] ?? ‘default value’;
You can use the same without assigning it into a variable, so as you would expect, it works with functions as well, like:
echo $_GET['something'] ?? 'default value';
Or other functions like implode()
, here is an example that prints comma separated list out of an array:
echo implode(', ', $usually_array ?? array()), PHP_EOL;
Above ensures implode()
will always receive an array, even an empty one, but it will be happy with that as well. If you wonder what that last “, PHP_EOL” does, it just adds a new line (enter) to the HTML source code, that in some cases makes it easier to read if you need to do some debugging later. I always try to generate as readable HTML as possible, and therefore I try to inject new lines with PHP when it makes sense from HTML point of view, even it is not visible for the end user.
30.7.2024 update: You can even chain multiple Null Coalescing Operators together:
$your_var = $_GET['your_var'] ?? $_POST['your_var'] ?? '';
Above will first try to get your_var from GET inputs, if not succeeding, then trying to get that from POST inputs and very last uses empty string if POST input was not available.
2.10.2024 update: You need to use parenthesis () with Null Coalescing Operator when joining string parts together like first row shows:
$str = 'Hello ' . ($name ?? 'Anonymous') . ' and welcome!';
$str = 'Hello ' . $name ?? 'Anonymous' . ' and welcome!';
If you don’t use parenthesis, then the Null Coalescing Operator will not work as you expect, meaning checking only the variable. Second row would return only “Anonymous and welcome!” if $name is NULL.