I am learning PHP from the O'reilly media book, "PHP Programming," and I came across this:
function add_up ($running_total, $current_value) { $running_total += $current_value * $current_value; return $running_total; } $numbers = array(2, 3, 5, 7); $total = array_reduce($numbers, 'add_up'); echo $total;
The array_reduce () line executes these function calls:
add_up(2,3) add_up(11,5) add_up(36,7) // $total is now 87
But when I count, I get 85. I think he should write like this:
The array_reduce( ) executes these function calls:
add_up (0,2); add_up (4,3); add_up (13,5); add_up (38,7);
Since the optional value of $ initial is set to NULL by default.
mixed array_reduce ( array $input , callable $function [, mixed $initial = NULL ] )
Can someone with more knowledge explain to me who is wrong and why?
boksa source share