Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Getting rid of null

Matthias Noback published the second article in his programming guidelines series. This time he explains why using null in a function isn’t a good idea. In my opinion it’s great advice that most devs can immediately apply in their projects.

It’s certainly a good idea to get rid of the uncertainty and vagueness that null brings to your code. Besides, most of the time when you encounter an actual null value in your program, you probably weren’t expecting it. You just call a method on it, thinking that it is an object and PHP will rightfully let your program crash.

Every particular null situation requires a different solution, but at least I’ll list several common solutions for you.


Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band. He loves waffles and butterflies.