Wednesday, 28 April 2021

Php: Anonymous function

Anonymous functions do not have a name and created at run time when you want to perform a task. Anonymous functions are also called as ‘callback functions’, ‘closures’.

 

How to define anonymous function?

Using ‘function’ keyword, you can define an anonymous function.

 

Example

function($a, $b){

  return $a + $b;

};

 

Let’s define a function that takes a function as argument and apply this function to remaining arguments.

 

function apply_this_function($a, $b, $my_function){

  return $my_function($a, $b);

}

 

Now we can use ‘apply_this_function’ to calculate sum of two numbers.

 

$sum_of_10_20 = apply_this_function(10, 20, function($a, $b){

  return $a + $b;

});

 

anonymous_function_demo.php

#!/usr/bin/php

<?php
   function apply_this_function($a, $b, $my_function){
       return $my_function($a, $b);
   }

   $sum_of_10_20 = apply_this_function(10, 20, function($a, $b){
       return $a + $b;
   });

   $difference_of_10_20 = apply_this_function(10, 20, function($a, $b){
        return $a - $b;
    });

    $multiplication_of_10_20 = apply_this_function(10, 20, function($a, $b){
        return $a * $b;
    });

    $division_of_10_20 = apply_this_function(10, 20, function($a, $b){
        return $a / $b;
    });

   echo "Sum of 10 and 20 is $sum_of_10_20\n";
   echo "Difference of 10 and 20 is $difference_of_10_20\n";
   echo "Multiplication of 10 and 20 is $multiplication_of_10_20\n";
   echo "Division of 10 and 20 is $division_of_10_20\n";

?>

 

Output

$./anonymous_function_demo.php 

Sum of 10 and 20 is 30
Difference of 10 and 20 is -10
Multiplication of 10 and 20 is 200
Division of 10 and 20 is 0.5

 

Many built-in functions use anonymous functions. For example, usort function takes an array and an anonymous functions as argument and sort the array elements based on the logic written in anonymous function.

 

Example 1: Sort elements in ascending order

usort($arr1, function($a, $b){

    return $a <=> $b;

});

 

Example 2: Sort elements in descending order

usort($arr1, function($a, $b){

  return $b <=> $a;

});

 

Find the below working application.

 

anonymous_function_demo_1.php

#!/usr/bin/php

<?php
    $arr1 = array(1, 34, 54, 23, 12, 31);

    echo 'Elements of array arr1 are: ';
    print_r($arr1);

    // Sort elements in ascending order
    echo "\nSort elements in ascending order\n";
    usort($arr1, function($a, $b){
        return $a <=> $b;
    });

    echo 'Elements of array arr1 are: ';
    print_r($arr1);

    // Sort elements in descending order
    echo "\nSort elements in descending order\n";
    usort($arr1, function($a, $b){
        return $b <=> $a;
    });

    echo 'Elements of array arr1 are: ';
    print_r($arr1);

?>

Output

$./anonymous_function_demo_1.php 

Elements of array arr1 are: Array
(
    [0] => 1
    [1] => 34
    [2] => 54
    [3] => 23
    [4] => 12
    [5] => 31
)

Sort elements in ascending order
Elements of array arr1 are: Array
(
    [0] => 1
    [1] => 12
    [2] => 23
    [3] => 31
    [4] => 34
    [5] => 54
)

Sort elements in descending order
Elements of array arr1 are: Array
(
    [0] => 54
    [1] => 34
    [2] => 31
    [3] => 23
    [4] => 12
    [5] => 1
)





 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment