• Tutorial List

Anonymous Function in PHP

Last updated May 15, 2020
Anonymous Function in PHP

What are the Anonymous functions in PHP?

Those functions are Anonymous functions which have no specified name. The Anonymous functions, also known as closures & they are most useful as the value of callback parameters.


👉 Syntax of the PHP Anonymous Function

📝 Anonymous functions behave like a value, that’s why you have to add a semicolon after ending the function or sometimes you have to add comma or nothing.

<?php
    function(){
        //Code To Be Executed;
    };
?>

In PHP, Anonymous functions are works like a normal function and behave like a value/expression.


👉 How to assign an anonymous function to a variable?

If you want to call an anonymous function, you have to assign this function to a variable.

<?php
    $myFunc = function(){
        echo "Hello I'm an Anonymous Function";
    };
    //  CALLING THE Anonymous Function
    $myFunc();
?>
Browser Output
Hello I’m an Anonymous Function

Passing arguments & Inheriting variables

👉 Passing Arguments

<?php
    $name = "John Doe";

    $myFunc = function($x){
        echo "Hello, $x";
    };

    $myFunc($name);
?>
Browser Output
Hello, John Doe

👉 Inheriting variables

If you want to Inherit a variable from the parent scope, you have to use the "use" language construct.

<?php
    $name = "John Doe";

    $myFunc = function() use($name){
        echo "Hello, $name";
    };

    $myFunc();
?>
Browser Output
Hello, John Doe

How to pass an Anonymous Function as a function argument?

We have already learned that anonymous functions behave like a value, that’s why you can pass anonymous functions as function arguments.

<?php
    function myFunc($theFunc){
  		/*
            Inside of the $theFunc
            
            $theFunc = function(){
                return "Hello, From Callback";
            };
        */
  
  		// calling the Arg Function
        echo $theFunc();
    }

    myFunc(function(){
        return "Hello, From Callback";
    });
?>
Browser Output
Hello, From Callback

How to return Anonymous Function?

you can also return an anonymous function from a function.

<?php
    function myFunc(){
        return function(){
            return "Hello";
        };
    }

    /* 
        Inside of the $sayHello -

        $sayHello = function(){
            return "Hello";
        };
    */

    $sayHello = myFunc();
    echo $sayHello();
?>
Browser Output
Hello

PHP Anonymous function in an array

You can add anonymous function to an array value.

<?php
    $myArray = [
        'SayName' => function($name){
            return $name;
        },
        'age' => 21
    ];

    $sayName = $myArray['SayName'];
    echo $sayName("John Doe");
?>
Browser Output
John Doe

PHP IFI Function

If you want to call an anonymous function without assign to a variable or passing to arguments, you can do that by the helping of IFI (Immediate function invocation).

<?php
    (function(){
        echo "Hello IFI";
    })();
?>
Browser Output
Hello IFI