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