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