1--TEST-- 2Test usort() function : usage variations - Anonymous comparison function 3--FILE-- 4<?php 5/* Prototype : bool usort(array $array_arg, string $cmp_function) 6 * Description: Sort an array by values using a user-defined comparison function 7 * Source code: ext/standard/array.c 8 */ 9 10/* 11 * Pass an anonymous comparison function as $cmp_function argument to test behaviour() 12 */ 13 14echo "*** Testing usort() : usage variation ***\n"; 15 16$cmp_function = function($value1, $value2) { 17 if ($value1 == $value2) { return 0; } 18 else if ($value1 > $value2) { return 1; } 19 else { return -1; } 20}; 21 22$array_arg = array(0 => 100, 1 => 3, 2 => -70, 3 => 24, 4 => 90); 23 24echo "\n-- Anonymous 'cmp_function' with parameters passed by value --\n"; 25var_dump( usort($array_arg, $cmp_function) ); 26var_dump($array_arg); 27 28$array_arg = array("b" => "Banana", "m" => "Mango", "a" => "Apple", "p" => "Pineapple"); 29 30$cmp_function = function(&$value1, &$value2) { 31 if ($value1 == $value2) { return 0; } 32 else if ($value1 > $value2) { return 1; } 33 else { return -1; } 34}; 35 36echo "\n-- Anonymous 'cmp_function' with parameters passed by reference --\n"; 37var_dump( usort($array_arg, $cmp_function) ); 38var_dump($array_arg); 39?> 40===DONE=== 41--EXPECTF-- 42*** Testing usort() : usage variation *** 43 44-- Anonymous 'cmp_function' with parameters passed by value -- 45bool(true) 46array(5) { 47 [0]=> 48 int(-70) 49 [1]=> 50 int(3) 51 [2]=> 52 int(24) 53 [3]=> 54 int(90) 55 [4]=> 56 int(100) 57} 58 59-- Anonymous 'cmp_function' with parameters passed by reference -- 60bool(true) 61array(4) { 62 [0]=> 63 string(5) "Apple" 64 [1]=> 65 string(6) "Banana" 66 [2]=> 67 string(5) "Mango" 68 [3]=> 69 string(9) "Pineapple" 70} 71===DONE=== 72