October 29, 2013 —John Koster
PHP version 5.3 introduced a new feature called anonymous functions, or lambda functions. You may have come across them in other languages such as C# or Ruby. They allow for greater flexibility and offer a clean, expressive syntax.
Anonymous functions are passed as arguments to other functions, and in this context they are usually referred to as callbacks. Let's look at a really simple example of this using PHP's array_filter
function:
1<?php 2 3// This is just creating a really simple array. 4$myArray = array('a', 'really', 'simple', 'array'); 5 6// Call the array_filter function using our really simple array and a 7// custom callback. 8$myNewArray = array_filter($myArray, function($value) { 9 return $value != 'a';10});
If we run the above code our really simple array will only have three values: 'really', 'simple', 'array'
. This is because in our callback (anonymous function) we said that if the $value != 'a'
to return true (when $value is 'a' our expression returns false). When we return true
from our anonymous function, the array_filter
function will keep the value in the array, and if we return false
from our anonymous function the value is not added to our new array.
For the purpose of this post, let's say we wanted to store the value to check against in its own variable like this:
1<?php 2 3// This is just creating a really simple array. 4$myArray = array('a', 'really', 'simple', 'array'); 5 6 7$valueToCheckAgainst = 'a'; 8 9// Call the array_filter function using our really simple array and a10// custom callback.11$myNewArray = array_filter($myArray, function($value) {12 return $value != $valueToCheckAgainst;13});
If we try to run this we code we will get an error similar to this:
Notice: Undefined variable: valueToCheckAgainst
But how come? We obviously declared the variable and we gave it a value! Surely PHP must be playing tricks on us!
Drama aside, the reason PHP gives us the error is because anonymous functions execute in a different scope than our regular code so our anonymous function cannot 'see' any of the variables we've declared outside of the anonymous function. To resolve this, we must use the use
keyword. That looks like this:
1<?php 2 3// This is just creating a really simple array. 4$myArray = array('a', 'really', 'simple', 'array'); 5 6 7$valueToCheckAgainst = 'a'; 8 9// Call the array_filter function using our really simple array and a10// custom callback.11$myNewArray = array_filter($myArray, function($value) use ($valueToCheckAgainst) {12 return $value != $valueToCheckAgainst;13});
That's all we have to do to make a variable available to our anonymous function when we cannot alter the number of parameters or anonymous function can have.
Tip: you can pass multiple variables to your anonymous function with the use
keyword by separating them with a comma:
1<?php 2 3// This is just creating a really simple array. 4$myArray = array('a', 'really', 'simple', 'array'); 5 6 7$valueToCheckAgainst = 'a'; 8$valueToCheckAgainst2 = 'b'; 9$valueToCheckAgainst3 = 'c';10 11// Call the array_filter function using our really simple array and a12// custom callback.13$myNewArray = array_filter($myArray, function($value) use ($valueToCheckAgainst, $valueToCheckAgainst2, $valueToCheckAgainst3) {14 return in_array($value, [15 $valueToCheckAgainst, $valueToCheckAgainst2, $valueToCheckAgainst316 ]);17});
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.