1--TEST-- 2Test usort() function : usage variations - Pass different data types as $array_arg arg 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 different data types as $array_arg argument to usort() to test behaviour 12 */ 13 14echo "*** Testing usort() : usage variations ***\n"; 15 16// Initialise function arguments not being substituted 17function cmp_function($value1, $value2) 18{ 19 if($value1 == $value2) { 20 return 0; 21 } 22 else if($value1 > $value2) { 23 return 1; 24 } 25 else { 26 return -1; 27 } 28} 29 30//get an unset variable 31$unset_var = 10; 32unset ($unset_var); 33 34// get a class 35class classA 36{ 37 public function __toString() { 38 return "Class A object"; 39 } 40} 41 42// heredoc string 43$heredoc = <<<EOT 44hello world 45EOT; 46 47// get a resource variable 48$fp = fopen(__FILE__, "r"); 49 50// unexpected values to be passed to $array_arg argument 51$inputs = array( 52 53 // int data 54/*1*/ 0, 55 1, 56 12345, 57 -2345, 58 59 // float data 60/*5*/ 10.5, 61 -10.5, 62 12.3456789000e10, 63 12.3456789000E-10, 64 .5, 65 66 // null data 67/*10*/ NULL, 68 null, 69 70 // boolean data 71/*12*/ true, 72 false, 73 TRUE, 74 FALSE, 75 76 // empty data 77/*16*/ "", 78 '', 79 array(), 80 81 // string data 82/*19*/ "string", 83 'string', 84 $heredoc, 85 86 // object data 87/*22*/ new classA(), 88 89 // undefined data 90/*23*/ @$undefined_var, 91 92 // unset data 93/*24*/ @$unset_var, 94 95 // resource variable 96/*25*/ $fp 97); 98 99// loop through each element of $inputs to check the behavior of usort() 100$iterator = 1; 101foreach($inputs as $input) { 102 echo "\n-- Iteration $iterator --\n"; 103 var_dump( usort($input, 'cmp_function') ); 104 $iterator++; 105}; 106 107//closing resource 108fclose($fp); 109?> 110===DONE=== 111--EXPECTF-- 112*** Testing usort() : usage variations *** 113 114-- Iteration 1 -- 115 116Warning: usort() expects parameter 1 to be array, integer given in %s on line %d 117NULL 118 119-- Iteration 2 -- 120 121Warning: usort() expects parameter 1 to be array, integer given in %s on line %d 122NULL 123 124-- Iteration 3 -- 125 126Warning: usort() expects parameter 1 to be array, integer given in %s on line %d 127NULL 128 129-- Iteration 4 -- 130 131Warning: usort() expects parameter 1 to be array, integer given in %s on line %d 132NULL 133 134-- Iteration 5 -- 135 136Warning: usort() expects parameter 1 to be array, float given in %s on line %d 137NULL 138 139-- Iteration 6 -- 140 141Warning: usort() expects parameter 1 to be array, float given in %s on line %d 142NULL 143 144-- Iteration 7 -- 145 146Warning: usort() expects parameter 1 to be array, float given in %s on line %d 147NULL 148 149-- Iteration 8 -- 150 151Warning: usort() expects parameter 1 to be array, float given in %s on line %d 152NULL 153 154-- Iteration 9 -- 155 156Warning: usort() expects parameter 1 to be array, float given in %s on line %d 157NULL 158 159-- Iteration 10 -- 160 161Warning: usort() expects parameter 1 to be array, null given in %s on line %d 162NULL 163 164-- Iteration 11 -- 165 166Warning: usort() expects parameter 1 to be array, null given in %s on line %d 167NULL 168 169-- Iteration 12 -- 170 171Warning: usort() expects parameter 1 to be array, boolean given in %s on line %d 172NULL 173 174-- Iteration 13 -- 175 176Warning: usort() expects parameter 1 to be array, boolean given in %s on line %d 177NULL 178 179-- Iteration 14 -- 180 181Warning: usort() expects parameter 1 to be array, boolean given in %s on line %d 182NULL 183 184-- Iteration 15 -- 185 186Warning: usort() expects parameter 1 to be array, boolean given in %s on line %d 187NULL 188 189-- Iteration 16 -- 190 191Warning: usort() expects parameter 1 to be array, string given in %s on line %d 192NULL 193 194-- Iteration 17 -- 195 196Warning: usort() expects parameter 1 to be array, string given in %s on line %d 197NULL 198 199-- Iteration 18 -- 200bool(true) 201 202-- Iteration 19 -- 203 204Warning: usort() expects parameter 1 to be array, string given in %s on line %d 205NULL 206 207-- Iteration 20 -- 208 209Warning: usort() expects parameter 1 to be array, string given in %s on line %d 210NULL 211 212-- Iteration 21 -- 213 214Warning: usort() expects parameter 1 to be array, string given in %s on line %d 215NULL 216 217-- Iteration 22 -- 218 219Warning: usort() expects parameter 1 to be array, object given in %s on line %d 220NULL 221 222-- Iteration 23 -- 223 224Warning: usort() expects parameter 1 to be array, null given in %s on line %d 225NULL 226 227-- Iteration 24 -- 228 229Warning: usort() expects parameter 1 to be array, null given in %s on line %d 230NULL 231 232-- Iteration 25 -- 233 234Warning: usort() expects parameter 1 to be array, resource given in %s on line %d 235NULL 236===DONE=== 237