CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

PHP - Passing Functions

PHP - Passing Functions

To a function in PHP, in addition to scalar types, arrays, and objects, you can also pass a function as one of its arguments. If a function is defined to accept another function as an argument, the passed function will be invoked inside it. PHPs standard library has certain built-in functions of this type, where one of the arguments to be passed is a function, which may be another built-in function or even a user defined function.

Using array_map() to Pass a Function

The array_map() is one of the built-in functions. The first argument to this function is a callback function. There may be one or more arrays as the other arguments. The callback function is applied to all the elements of arrays.

PHP CODE
array_map(?callable $callback, array $array, array ...$arrays): array

The array_map() function returns an array. It contains the result of applying the callback function to the corresponding elements of arrays passed as other arguments.

Example

In the following example, we have a square() function that computes the square of a number passed to it. This function in turn is used as an argument for array_map() function, along with another array of numbers. Each number is successively passed to the squares() function. The resultant array is a list of squares.

PHP CODE
<?php
   function square($number) {
      return $number * $number;
   }

   $arr = [1, 2, 3, 4, 5];
   $squares = array_map('square', $arr);
   var_dump($squares);
?>

Output

It will produce the following output βˆ’

PHP CODE
array(5) {
   [0]=>
   int(1)
   [1]=>
   int(4)
   [2]=>
   int(9)
   [3]=>
   int(16)
   [4]=>
   int(25)
}

Usage of call_user_func() Function

Another example of passing a function to another function is call_user_func(). As the name suggests, it calls another user defined callback function, and the other arguments are passed to the callback.

PHP CODE
call_user_func(callable $callback, mixed ...$args): mixed

Example

In the example below, the square() function is invoked repeatedly, passing each number in an array.

PHP CODE
<?php
   function square($number) {
      return $number * $number;
   }
   $arr = [1, 2, 3, 4, 5];
   foreach($arr as $a) {
      echo "square of $a:" . call_user_func("square", $a). PHP_EOL;
   }
?>

Output

It will produce the following output βˆ’

PHP CODE
square of 1:1
square of 2:4
square of 3:9
square of 4:16
square of 5:25

Usage of usort() Function

As another example of passing function, we take a look a usort() function.

PHP CODE
usort(array &$array, callable $callback): true

The first parameter is an array. The array is sorted as per the callback function, which is the second parameter.

The callback parameter is a comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Example

Here is an example. First we have amysort()function. It compares two numbers and returns β€œ-1”, β€œ0” or β€œ1” if the first number is less than, equal to or greater than second number.

The first argument tousort()is the mysort() function, and the second one is an array. To begin with, the first two numbers are passed to mysort(). If it returns 1, they are swapped. Next, the second and third numbers are passed and swapped if the comparison returns 1. The same process repeats so that the array elements are arranged in ascending order.

PHP CODE
<?php
   function mysort($a, $b) {
      if ($a == $b) {
         return 0;
      }
      return ($a < $b) ? -1 : 1;
   }

   $a = array(3, 2, 5, 6, 1);

   usort($a, "mysort");

   foreach ($a as $key => $value) {
      echo "$key: $value\n";
   }
?>

Output

It will produce the following output βˆ’

PHP CODE
0: 1
1: 2
2: 3
3: 5
4: 6

Pass Callback to User-defined Function

Apart from the above built-in functions, you can define your own function that accepts one of the arguments as another function.

In the example below, we have two functions,square()andcube(), that return the square and cube of a given number.

Next, there ismyfunction(), whose first argument is used as a variable function and the second argument tomyfunction()is passed to it.

Thus, myfunction() internally calls square() or cube() to return either square or cube of a given number.

Example

PHP CODE
<?php
   function myfunction($function, $number) {
      $result = $function($number);
      return $result;
   }

   function cube($number) {
      return $number ** 2;
   }

   function square($number) {
      return $number ** 3;
   }

   $x = 5;

   $cube = myfunction('cube', $x);
   $square = myfunction('square', $x);

   echo "Square of $x = $square" . PHP_EOL;
   echo "Cube of $x = $cube" . PHP_EOL;
?>

Output

It will produce the following output βˆ’

PHP CODE
Square of 5 = 125
Cube of 5 = 25