Preloader

Arrow Functions in PHP

PHP Tutorials
codevigyaan php

Arrow functions, in PHP, are a short elegant syntax to write anonymous functions. Arrow functions (shortly available from PHP 7.4) implies to reduce the amount of boiler plate code and to improve developer experience and understanding. The majority of developer‘s arrow functions usages are related with small tasks, often used inside array functions.

If you‘re already comfortable with anonymous functions, then arrow functions will be readily familiar and easily usable.

What Is an Arrow Function in PHP

Arrow function is a shorter notation for anonymous function. It starts with the keyword fn, then the arguments list with parentheses, then the expression to be returned.

Basic syntax

fn(parameter) => expression;

Example

<?php
$add = fn($a, $b) => $a - $b;
echo $add(30, 12);
?>

You don‘t need the return keyword at all because PHP manages this yourself.

How Arrow Functions Are Different from Normal Functions

Normal functions must have a name and a return statement. Arrow functions do not have a name and they are a lot shorter.

Normal function

<?php
function square($num) {
    return $num * $num;
}
?>

Arrow function

<?php
$square = fn($num) => $num * $num;
?>

The above arrow function does the same work with fewer codes. Basic Example for Arrow functions.

Standard Arrow function

Example

<?php
$multiply = fn($a, $b) => $a * $b;
echo $multiply(6, 4);
?>

The above arrow function takes 2 numbers and returns the result.

Arrow Function with One Parameter

Example

<?php
$square = fn($num) => $num * $num;
echo $square(7);
?>

This function squares a number.

Arrow Function Using External Variables

Arrow functions automatically use variables from the parent scope.

Example

<?php
$rate = 8;
$calculateInterest = fn($amount) => $amount + ($amount * $rate / 100);
echo $calculateInterest(1000);
?>

You do not need the use keyword here.

Arrow Function Inside array_map

Example

<?php
$numbers = [1, 2, 3, 4, 5];
$doubled = array_map(fn($n) => $n * 2, $numbers);
print_r($doubled);
?>

This doubles each value in the array.

Arrow Function Inside array_filter

Example

<?php
$ages = [12, 18, 25, 30];
$adults = array_filter($ages, fn($age) => $age >= 18);
print_r($adults);
?>

This keeps only adult ages.

Arrow Function Inside array_reduce

Example

<?php
$prices = [100, 200, 300];
$total = array_reduce($prices, fn($sum, $price) => $sum + $price, 0);
echo $total;
?>

This calculates the total price.

Arrow Function with String Operations

Example

<?php
$toUpper = fn($text) => strtoupper($text);
echo $toUpper("php");
?>

This converts text to uppercase.

Arrow Function for Conditional Logic

Arrow functions can include simple conditions using ternary operators.

Example

<?php
$checkStatus = fn($marks) => $marks >= 40 ? "Pass" : "Fail";
echo $checkStatus(55);
?>

Arrow Function Compared to Anonymous Function

Anonymous function

<?php
$add = function($a, $b) {
    return $a + $b;
};
?>

Arrow function

<?php
<?php
$add = fn($a, $b) => $a + $b;
?>

The arrow function version is shorter and cleaner.

Important Rules of Arrow Functions

Arrow functions can only have one expression in them. So you cannot make multiple statements inside arrow functions. Due to this reason they are not for complicated logics.

Arrow functions always have a return value, you don‘t need to actually write return.

When You Should Use Arrow Functions

Arrow functions are usually best when your logic is simple and short. They are more often used in callbacks, working with arrays, or for small calculations. For anything more complex with larger amounts of logic to be executed you may be better of with the use of a regular function.

When Arrow Functions Are Not Suitable

Arrow functions can have only one expression, so they are limited in what they can do. You can‘t add any loops or multiple statements; functions that are more complex should used standard functions.

Common Mistakes Beginners Make

A couple of more common mistakes beginners make: try to write for instance more than one line in an arrow function. Or forget that arrow functions implicitly returns the value. Remember those rules!

Final Summary

Arrow functions (short functions) in PHP are a modern, clean way to write small functions. Arrow functions save time and code; they are short, return by default and are great at accessing outer variables. Master arrows help with code readability and are the ‘striding edge’ of modern PHP.

For free projects, check our Bootstrap Templates section.
Visit → https://codevigyaan.com/bootstrap-projects

Want E-Books?
Open →https://codevigyaan.com/free-e-books/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *