Eros functions were introduced in PHP 7.4 to allow the gods to write short, anonymous functions. They offer a compact alternative to traditional sanctions, especially when the function body is small and focused.
In this article, you will learn how to use the aero function in PHP with examples. You will also learn about the difference between arrow functions and anonymous functions.
The table of content
Provisions
You should know that the basic PHP code, such as writing functions, and should be able to work with the rows. Make sure you are using PHP 7.4 or new because arrow functions work only in and above this version.
Understand the functions of the arrow in the pHP
PHP Arrow Function There is a short hand syntax. It explains an anonymous function and is designed for easy tasks and impression.
PHP arrow functions are best used when:
You need a quick callback or an inline function.
The function returns the same expression.
You want to avoid repeatedly
use
Statements
The main syntax of arrow function is:
fn(parameter_list) => expression;
fn
It is the key word that describes the arrow function.parameter_list
The parameters list is (like normal function).=>
Separates the parameter list from expression.expression
The function is the value of looting. You can’t use a block of statements here – only one expression is allowed.
Arrow functions automatically capture variables from the scope. They do not need use
Key word as shown below:
$var_name = 10;
$func = function($n) use ( $var_name ) {
return $n * $var_name;
}
You can use variables in direct scope:
$var_name = 10;
$func = fn($n) => $n * $var_name;
Here is a literal scooping example:
$multiplier = 3;
$multiply = fn($x) => $x * $multiplier;
echo $multiply(4);
Variable $multiplier
The outer scope is automatically caught. You don’t have to use use($multiplier)
As you want in the traditional anonymous function.
The Key Rules of the Airo Function Syntax:
Always use
fn
Notfunction
.No curly curvy
return
Key word – only one expression.Automatic variable capture from external circle.
This may not contain multiple statements or control structures (such as
if
For, for, for,.foreach
And so).
Let’s move towards the following part to take a look at the difference between arrow functions and the difference between Anonymous functions in pHP.
Difference between arrow functions and anonymous functions in pHP
PHP supports two types of anonymous functions (ie functions without name):
Both types can be assigned for variables and can be used for callbacks or as a function arguments. They fulfill similar goals, but are different in syntax and how they handle external variables.
Let’s look at their key differences.
1. The syntax
Arrow Function:
Arrow functions use a line expression of bouncing without any curves return
Description
$square = fn($n) => $n * $n;
Arrow function assigns it to variable $square
. The function takes a parameter, $n
And looted $n * $n
(Square $n
,
Anonymous Function:
$square = function($n) {
return $n * $n;
};
Anonymous functions use a complete function block and require clear return
. They are used for multi -line logic or complex behavior.
2. Differential circle (literal circle)
Arrow functions automatically capture variables from the outer scope:
$factor = 2;
$multiply = fn($x) => $x * $factor;
Anonymous functions you need to use external variables by using use
:
$factor = 2;
$multiply = function($x) use ($factor) {
return $x * $factor;
};
You can’t use variables in the scope of anonymous function unless you use it use
Key word
3. Reading qualification and race
Arrow functions are low. They help you write small and single express callbacks:
$numbers = (1, 2, 3);
$squares = array_map(fn($n) => $n * $n, $numbers);
But when anonymous functions are better:
Here is a table that shows you the key differences:
Feature | Arrow function | Anonymous Function |
I introduced | PHP 7.4 | PHP 5.3 |
Syrup | Short, single expression | Verb, complete function body |
Handling the circle | Automatic (literal) | Manual (use ) Keywords |
Multi -line body | Is not allowed | Allowed |
Return the keyword | Not used | Is necessary |
Let’s go to the bottom to understand how to return the arrow function from another function.
How to return arrow functions from other functions
Functions are first class citizens. This means that you can return a function from another function. This includes arrow functions.
You can describe and return the arrow function from within such a regular function:
function getMultiplier($factor) {
return fn($x) => $x * $factor;
}
$double = getMultiplier(2);
echo $double(5);
In this instance:
getMultiplier()
Return an arrow function.Arrow’s function grips
$factor
Automatically (literal scooping) from the external scope.The returning function can be stored in the variable and can be used as another collar.
It allows you to develop small functions based on parameters and reduce the code repetition.
Use this syntax when you need to make dynamic behavior or functioning factories like a function factories.
Let’s go to the part below to see how you can use the arrow functions in your code.
How to use arrow functions in your code
Use an arrow function inside her array_map()
:
array_map()
Lets you set a call back on every element of the row. This allows you to describe the callback directly inside the function call.
Example:
$numbers = (1, 2, 3, 4, 5);
$squares = array_map(fn($n) => $n * $n, $numbers);
print_r($squares);
Arrow function fn($n) => $n * $n
Are hanged for each element of $numbers
The source result is a new row of square values.
Use an arrow function with arrows array_filter()
array_filter()
Filters the elements of a row in the callback. Arrow functions describe the condition of a short filter inline.
Example:
$numbers = (1, 2, 3, 4, 5, 6);
$evenNumbers = array_filter($numbers, fn($n) => $n % 2 === 0);
print_r($evenNumbers);
Here, the arrow function checks whether there is every number. The result is a row that only contains the number.
Use an arrow function with arrows array_reduce()
array_reduce()
Callback reduces a row at the same price based on the function. Arrow functions help to compact the code.
Example:
$numbers = (1, 2, 3, 4, 5);
$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);
echo $sum;
The arrow function adds each number to the row. $carry
Running tomorrow and $n
The current number.
The functions of the nest in the pHP
Here the internal function performs an operation and the external function takes the results of the internal function.
$numbers = (1, 2, 3, 4, 5);
$doubleAndSquare = array_map(
fn($n) => fn($x) => ($x * 2) ** 2,
$numbers
);
$results = array_map(
fn($fn) => $fn(3),
$doubleAndSquare
);
print_r($results);
In the aforementioned code, first array_map()
He produces a list of arrow functions that double and then square the number. In each element $numbers
The array of nests makes a map in the aero function.
Second array_map()
Applies the number to the inner arrow function (which doubles and squares the value) 3
. As a result, there is a row of the same result.
Wrap
In this article, you have learned the basic features and syntax of the aero functions. This shows you their benefits more than anonymous functions.
Here are some important paths:
Arrow functions were introduced in PHP 7.4. They provide you a new syntax to explain anonymous functions with easy code.
An anonymous way to write anonymous functions is arrow functions. They use a line of code and do not need curvy curvy bracelets
return
Key wordArrow functions automatically gain variable. This gives you the convenience of using arrows function as a callback in the function
array_map()
Orarray_filter()
.
Resources: