PHP Anonymous functions with examples in detail

Spread the love

PHP Anonymous functions

In this chapter, you will learn another useful feature of PHP which is PHP Anonymous functions.

An anonymous function is simply a PHP function but as its name suggests, it’s anonymous, which means it’s not having any name.

Then how can we use it?

But first, let’s see how it looks.

A normal function in PHP has the name of a function and is called by using that name, like below.

Basic PHP function

<?php
 function hello()
 {
  echo "Hello from PHP.";
 }
 
 hello();
?>

Now let’s see what the anonymous functions look like.

 

PHP Anonymous functions

<?php
 function()
 {
  echo "Hello from anynomous fuction in PHP.";
 };
 
?>

As per the above example, a PHP Anonymous function has the following features:

  1. There is no name present between the “function” keyword and the parenthesis ().
  2. After the end of curly braces { }, there is a semicolon present.

The semicolon at the end of a function states that it is an expression.

But how to call the anonymous function as it does not have any name, and as a basic function example we can call the function only by its name?

 

The solution is Lambda

As the PHP Anonymous functions can be treated as an expression, its definition can be assigned to a variable. And then using that variable name you can call that function.

This is called Lambda. Lambda is just another name for an anonymous function.
Lambda is an anonymous function that can be assigned to a variable, also which can be sent/passed to another function as an argument.

 

What is the need for PHP Anonymous functions or a Lambda?

Lambdas are the use-and-throw functions that you can use once.

If you need the functionality, which you will use only once in a program or a certain functionality that will not be used in a whole program then, in that case, it would not be feasible to create a full global scoped function.

Such functions which are rarely used, can be replaced by Lambdas. Just create a function inside a block for that scope only.

Assigning PHP Anonymous functions to a variable

Just like a normal variable assignment, you can also assign this function using a simple assignment operator.

Let’s see the code:

<?php
 $hello = function()
 {
  echo "Hello from an anonymous fuction in PHP.";
 };
 
 $hello();
?>


The output will be:

Hello from an anonymous fuction in PHP.

As you see in the example I have assigned a function to the variable $hello.

And then using variable name and parenthesis, a function is called. Like $hello().

In this way, you can assign and call an anonymous function using a variable.

 

Passing parameters to a function

Just like the normal function, you can also send parameters to anonymous functions. The parameters are then collected inside the parenthesis using an argument variable.

<?php
 $hello = function($arg)
 {
  echo $arg;
 };
 
 $hello('Hello from PHP');
?>

Output:

Hello from PHP

This was a simple argument-passing program where we sent the string “Hello from PHP” to a function using $hello().

 

The scope of the variable in a function

Now in the next example, let’s define one more variable outside a function with some value. And try to access it directly inside PHP anonymous functions.

<?php

 $msg = "PHP is awesome";
 
 $hello = function()
 {
  echo $msg;
 };
 
 $hello();
?>

The above program will generate a Notice as an undefined variable msg.

You will get this notice because the function didn’t know what is the value of $msg inside it.

This happens because of the scope of the variable and the function. Both the scopes of functions and variables are different.

The variable declared outside the function body is not known to the function.

If you want to use that variable inside a function then one method is to send it with a function call, like:

$hello($msg);

Another way to use outside scoped variables is by using the PHP feature called Closure.

What is a Closure?

A Closure is the same as PHP Anonymous functions or lambdas but it can access variables defined outside the scope of where it was created.

You can access the variable present outside scope by declaring it in a “use” clause of the Clouser function.

<?php

 $msg = "PHP is awesome";
 
 $hello = function() use($msg)
 {
  echo $msg;
  
 };
 
 $hello($msg);
?>

As you see in the above example I have used $msg in a use clause like use($msg).

Now the variable $msg is accessible inside a function or a closure.

Pass by reference in a Closure

In the above example, the $msg variable used use() cannot be changed inside a function, because $msg is sent as a copy.

But if you want to change the value of a variable $msg inside a function you need to use an ampersand (&) before the $msg inside use() clause.

Let’s see the updated code:

<?php

 $msg = "PHP is awesome";
 
 $hello = function() use(&$msg)
 {
  echo $msg;
  $msg = "<br>Message changed inside a function.";
 };
 
 $hello($msg);
 
 echo $msg;
?>

The output will be:

PHP is awesome
Message changed inside a function.

In the above example, we have changed the value of $msg inside a function body. This only becomes possible due to the use of the ampersand (&).

Passing PHP Anonymous functions to another function

You can also send PHP anonymous functions to another function.

<?php

 function showMessage($msg)
 {
  $msg();
 }
 
 showMessage(function()
 {
  echo "Function sent to another function.";
 });
 
 
?>

In the above example, I have sent a function directly through a function call to another function “showMessage”.

Inside “showMessage” function definition $msg is used to call that sent function body using $msg();

Hope PHP Anonymous functions tutorial has helped you to learn about an anonymous function/Lambda and Closure. If you find any difficulty or have any questions please comment below.

You can find more about the PHP anonymous functions on the official PHP website.


Spread the love

Leave a Comment