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!

Functional programming in PHP

Almost a year ago Igor Wiedler wrote three articles on his blog about the state of functional programming in PHP.

The first article explores iteration. You’ll learn to turn this

$admins = [];
foreach ($users as $user) {
if (is_admin($user)) {
$admins[] = $user;

into this

use function iter\filter;
$admins = filter('project\user\is_admin', $users);

In the second one he explains a very nice way to traverse an associative array. How you currently do it:

$baz = (isset($data['foo']['bar']['baz'])) ? $data['foo']['bar']['baz'] : null;

How you’ll do it in the future:

use function igorw\get_in;
$baz = get_in($data, ['foo', 'bar', 'baz']);

The final article shows you a nice syntax to handle objects that return null-values.

All the articles mention libraries that you can use in your code today.

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.