]> granicus.if.org Git - php/blob
3689f8a798
[php] /
1 --TEST--
2 Test array_walk() function : usage variations - anonymous callback function
3 --FILE--
4 <?php
5 /* Prototype  : proto bool array_walk(array $input, string $funcname [, mixed $userdata])
6  * Description: Apply a user function to every member of an array
7  * Source code: ext/standard/array.c
8 */
9
10 /*
11 * Passing anonymous(run-time) callback function with following variations:
12 *   with one parameter
13 *   two parameters
14 *   three parameters
15 *   extra parameters
16 *   without parameters
17 */
18
19 echo "*** Testing array_walk() : anonymous function as callback ***\n";
20
21 $input = array(2, 5, 10, 0);
22
23 echo "-- Anonymous function with one argument --\n";
24 var_dump( array_walk($input, function($value) { var_dump($value); echo "\n"; }));
25
26 echo "-- Anonymous function with two arguments --\n";
27 var_dump( array_walk($input, function($value, $key) { var_dump($key); var_dump($value); echo "\n"; }));
28
29 echo "-- Anonymous function with three arguments --\n";
30 var_dump( array_walk($input, function($value, $key, $user_data) { var_dump($key); var_dump($value); var_dump($user_data); echo "\n"; }, 10));
31
32 echo "-- Anonymous function with null argument --\n";
33 var_dump( array_walk( $input, function() { echo "1\n"; }));
34 echo "Done"
35 ?>
36 --EXPECT--
37 *** Testing array_walk() : anonymous function as callback ***
38 -- Anonymous function with one argument --
39 int(2)
40
41 int(5)
42
43 int(10)
44
45 int(0)
46
47 bool(true)
48 -- Anonymous function with two arguments --
49 int(0)
50 int(2)
51
52 int(1)
53 int(5)
54
55 int(2)
56 int(10)
57
58 int(3)
59 int(0)
60
61 bool(true)
62 -- Anonymous function with three arguments --
63 int(0)
64 int(2)
65 int(10)
66
67 int(1)
68 int(5)
69 int(10)
70
71 int(2)
72 int(10)
73 int(10)
74
75 int(3)
76 int(0)
77 int(10)
78
79 bool(true)
80 -- Anonymous function with null argument --
81 1
82 1
83 1
84 1
85 bool(true)
86 Done