PHP introduced the arrow function syntax in version 7.4. It’s just a shorthand way to write anonymous functions, sort of. The main difference between anonymous functions and arrow functions in PHP is the use of the use keyword. In order to have access to the scope outside of the anonymous function, you must use the use keyword. Arrow functions automatically have access to scope outside of the function.
Anatomy of an arrow function.
<?php
$global_var = 5 ;
$arrow_func = fn ( $param ) => $param + $global_var ;
echo $arrow_func ( 5 );
?>
The arrow function starts with the shortened function keyword fn() ; parameters can be declared inside of the parentheses. The arrow (=>) comes immediately after the fn(), which signifies that whatever comes next will be returned. You cannot enter an expression like the echo statement here since the arrow function implicitly uses the return statement. The arrow function also has access to the global variable $global_var . We don’t have to implement the use keyword like we’ve done so far with anonymous functions . We could have used an anonymous function with the aid of the use keyword to accomplish the same task.
<?php
$global_var = 5 ;
$arrow_func = function ( $param ) use ( $global_var ) {
return $param + $gloabal_var ;
};
echo $arrow_func ( 5 );
?>
If you’ve read my article on the use keyword , you would know that:
use gets the value of the global variable when the function is defined and global will get the value of the variable when the function is called
So, even if we modified the global variable after the definition, but before the function call, the result would still stay the same.
?php
$global_var = 5 ;
$arrow_func = fn ( $param ) => $param + $global_var ;
echo $arrow_func ( 5 );
$global_var = 10 ;
echo $arrow_func ( 5 );
?>
In the example above, the arrow function is called on line 7. The arrow function returns 10 since it adds the value of the global variable, which is 5, to the value of the argument, which is also 5 . On line 9, the global variable’s value is changed to 10 . However, when the arrow function is called on line 11, it still displays 10 . If the global variable was retrieved upon function call, the value would be 15, but that’s not the case with arrow functions or anonymous functions that implicitly or explicitly utilize the use keyword.
Now that we have an understanding of how arrow functions work, let’s look at some more examples. We’ll start by creating a closure and then converting it into an arrow function.
<?php
$stuff_dogs_say = function () {
echo "Dog says Cats Suck<br>" ;
};
$stuff_dogs_say ();
?>
The closure is assigned to $stuff_dogs_say and is then called on line 7. The anonymous function echoes out Dog says Cays suck . To convert that into an arrow function is pretty straight forward. Remember, arrow functions only return a value; they cannot utilize the echo statement internally.
<?php
$stuff_dogs_say = fn () => "Dog says Cats Suck<br>" ;
echo $stuff_dogs_say ();
?>
We start by creating a variable $stuff_dogs_say and assigning the arrow function to it. The arrow function starts with fn() followed by the arrow. The string Dog says Cats Suck is returned implicitly. We just have to place the string immediately after the arrow syntax and the arrow function immediately returns it. We then utilize the echo statement with the function call on line 5 to execute the code.
Let’s look at another example. This time we’ll pass an argument to the arrow function.
<?php
$stuff_dogs_say = fn ($saying ) => "Dogs say " . $saying . "<br>" ;
echo $stuff_dogs_say ("Let Me Out" );
?>
We create an arrow function. The function is declared with the parameter $saying . Upon function call, the argument will be concatenated to the Dogs say string and returned.
PHP sees the echo statement on line 5 and begins execution.
It calls the $stuff_dogs_say arrow function and passes the Let Me Out string argument to it.
PHP enters the arrow function, concatenates the argument to the Dogs say string and returns the new string Dogs say Let Me Out .
PHP returns back to line 5 and echoes out the string Dogs say Let Me Out.
That should get you set with arrow functions. It’s really just a way to make smaller closures neater.