PHP has more than a thousand inbuilt functions and enables its developers to create their own functions as well. Functions are considered one of the extraordinary strengths of PHP; they are a structure of code that can be used repetitively. They need to be called in order to achieve their specific task.
Below, we see how to create a function, callEducative(),
which prints Welcome to Educative!
.
<?phpfunction callEducative() {echo "Welcome to Educative!";}callEducative(); // function call?>
reduce()
functionThis is an inbuilt function that applies operation using the callback function to reduce the set to a single value.
mixed public Ds\Set::reduce ( callable $callback[, mixed $initial ] )
The following code shows the implementation reduce()
function:
<?php$set = new \Ds\Set([10, 15, 25, 35, 40]);echo("Initial elements of the set\n");print_r($set);$f = function($carry, $element) {return $carry * $element;};echo("\nReduced to a single element\n");// reduce()var_dump($set->reduce($f, 10));?>
Initial elements of the set
Ds\Set Object
{
[0] => 10
[1] => 15
[2] => 25
[3] => 35
[4] => 40
}
Reduced to a single element
int(5250000)