1--TEST-- 2Test array_filter() function : usage variations - Different types of 'callback' function 3--FILE-- 4<?php 5/* Prototype : array array_filter(array $input [, callback $callback]) 6 * Description: Filters elements from the array via the callback. 7 * Source code: ext/standard/array.c 8*/ 9 10/* 11* Passing different types of callback functions to array_filter() 12* with parameters and return 13* without parameter and with return 14* with parameter and without return 15* without parameter and without return 16*/ 17 18echo "*** Testing array_filter() : usage variation - different 'callback' functions***\n"; 19 20// Initialize variables 21$input = array(0, -1, 2, 3.4E-3, 'hello', "value", "key" => 4, 'null' => NULL); 22 23// callback function without parameters and with return value 24function callback1() 25{ 26 return 1; 27} 28echo "-- Callback function without parameter and with return --\n"; 29var_dump( array_filter($input, "callback1") ); 30 31// callback function with parameter and without return value 32function callback2($input) 33{ 34} 35echo "-- Callback funciton with parameter and without return --\n"; 36var_dump( array_filter($input, "callback2") ); 37 38 39// callback function without parameter and without return value 40function callback3() 41{ 42} 43echo "-- Callback function without parameter and return --\n"; 44var_dump( array_filter($input, "callback3") ); 45 46// callback function with parameter and with return value 47function callback4($input) 48{ 49 if($input > 0 ) { 50 return true; 51 } 52 else { 53 return false; 54 } 55} 56echo "-- Callback function with parameter and return --\n"; 57var_dump( array_filter($input, "callback4") ); 58 59echo "Done" 60?> 61--EXPECTF-- 62*** Testing array_filter() : usage variation - different 'callback' functions*** 63-- Callback function without parameter and with return -- 64array(8) { 65 [0]=> 66 int(0) 67 [1]=> 68 int(-1) 69 [2]=> 70 int(2) 71 [3]=> 72 float(0.0034) 73 [4]=> 74 string(5) "hello" 75 [5]=> 76 string(5) "value" 77 ["key"]=> 78 int(4) 79 ["null"]=> 80 NULL 81} 82-- Callback funciton with parameter and without return -- 83array(0) { 84} 85-- Callback function without parameter and return -- 86array(0) { 87} 88-- Callback function with parameter and return -- 89array(3) { 90 [2]=> 91 int(2) 92 [3]=> 93 float(0.0034) 94 ["key"]=> 95 int(4) 96} 97Done 98