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 = 'if($value1 == $value2) {return 0;} else if($value1 > $value2) {return 1;} else{return -1;}'; 17 18$array_arg = array(0 => 100, 1 => 3, 2 => -70, 3 => 24, 4 => 90); 19 20echo "\n-- Anonymous 'cmp_function' with parameters passed by value --\n"; 21var_dump( usort($array_arg, create_function('$value1, $value2',$cmp_function) ) ); 22var_dump($array_arg); 23 24$array_arg = array("b" => "Banana", "m" => "Mango", "a" => "Apple", "p" => "Pineapple"); 25 26echo "\n-- Anonymous 'cmp_function' with parameters passed by reference --\n"; 27var_dump( usort($array_arg, create_function('&$value1, &$value2', $cmp_function) ) ); 28var_dump($array_arg); 29?> 30===DONE=== 31--EXPECTF-- 32*** Testing usort() : usage variation *** 33 34-- Anonymous 'cmp_function' with parameters passed by value -- 35bool(true) 36array(5) { 37 [0]=> 38 int(-70) 39 [1]=> 40 int(3) 41 [2]=> 42 int(24) 43 [3]=> 44 int(90) 45 [4]=> 46 int(100) 47} 48 49-- Anonymous 'cmp_function' with parameters passed by reference -- 50bool(true) 51array(4) { 52 [0]=> 53 string(5) "Apple" 54 [1]=> 55 string(6) "Banana" 56 [2]=> 57 string(5) "Mango" 58 [3]=> 59 string(9) "Pineapple" 60} 61===DONE===