xref: /PHP-7.1/ext/standard/tests/array/bug24980.phpt (revision 7af945e2)
1--TEST--
2Bug #24980 (array_reduce() uses first element as default running total)
3--FILE--
4<?php
5/* test #1: numeric data */
6function add_up($running_total, $current_value)
7{
8	echo "running_total is ".(int)$running_total.", current_value is {$current_value}\n";
9	$running_total += $current_value * $current_value;
10	return $running_total;
11}
12
13$numbers = array (2,3,5,7);
14$total = array_reduce($numbers, 'add_up');
15print "Total is $total\n";
16
17/* test #2: string data */
18$a = array("a", "b", "c");
19function foo ($a, $b)
20{
21	return $a . $b;
22}
23var_dump(array_reduce($a, "foo"));
24
25/* test #3: basic test (used to leak memory) */
26function rsum($v, $w)
27{
28	$v += $w;
29	return $v;
30}
31function rmul($v, $w)
32{
33	$v *= $w;
34	return $v;
35}
36$a = array(1, 2, 3, 4, 5);
37$x = array();
38$b = array_reduce($a, "rsum");
39$c = array_reduce($a, "rmul", 10);
40$d = array_reduce($x, "rsum", 1);
41
42var_dump($b, $c, $d);
43?>
44--EXPECT--
45running_total is 0, current_value is 2
46running_total is 4, current_value is 3
47running_total is 13, current_value is 5
48running_total is 38, current_value is 7
49Total is 87
50string(3) "abc"
51int(15)
52int(1200)
53int(1)
54