1--TEST--
2Test array_rand() function : usage variations - unexpected values for 'num_req' parameter
3--FILE--
4<?php
5/* Prototype  : mixed array_rand(array input [, int num_req])
6 * Description: Return key/keys for random entry/entries in the array
7 * Source code: ext/standard/array.c
8*/
9
10/*
11* Test array_rand() with different types of values other than int passed to 'num_req' argument
12* to see that function works with unexpeced data and generates warning message as required.
13*/
14
15echo "*** Testing array_rand() : unexpected values for 'num_req' parameter ***\n";
16
17// Initialise function arguments
18$input = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
19
20//get an unset variable
21$unset_var = 10;
22unset ($unset_var);
23
24//define a class
25class test
26{
27  var $t = 10;
28  function __toString()
29  {
30    return "3object";
31  }
32}
33
34//array of values to iterate over
35$values = 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        12.3456789000e10,
47        12.3456789000E-10,
48        .5,
49
50        // null data
51/*10*/  NULL,
52        null,
53
54        // boolean data
55/*12*/  true,
56        false,
57        TRUE,
58        FALSE,
59
60        // empty data
61/*16*/  "",
62        '',
63
64        // string data
65/*18*/  "string",
66        'string',
67
68        // object data
69/*20*/  new test(),
70
71        // undefined data
72/*21*/  @$undefined_var,
73
74        // unset data
75/*22*/  @$unset_var,
76);
77
78
79// loop through each element of the array for different values for 'num_req' argument
80$count = 1;
81foreach($values as $value) {
82  echo "\n-- Iteration $count --\n";
83  var_dump( array_rand($input,$value) );
84  $count++;
85};
86
87echo "Done";
88?>
89--EXPECTF--
90*** Testing array_rand() : unexpected values for 'num_req' parameter ***
91
92-- Iteration 1 --
93
94Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
95NULL
96
97-- Iteration 2 --
98int(%d)
99
100-- Iteration 3 --
101
102Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
103NULL
104
105-- Iteration 4 --
106
107Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
108NULL
109
110-- Iteration 5 --
111array(10) {
112  [0]=>
113  int(%d)
114  [1]=>
115  int(%d)
116  [2]=>
117  int(%d)
118  [3]=>
119  int(%d)
120  [4]=>
121  int(%d)
122  [5]=>
123  int(%d)
124  [6]=>
125  int(%d)
126  [7]=>
127  int(%d)
128  [8]=>
129  int(%d)
130  [9]=>
131  int(%d)
132}
133
134-- Iteration 6 --
135
136Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
137NULL
138
139-- Iteration 7 --
140
141Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
142NULL
143
144-- Iteration 8 --
145
146Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
147NULL
148
149-- Iteration 9 --
150
151Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
152NULL
153
154-- Iteration 10 --
155
156Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
157NULL
158
159-- Iteration 11 --
160
161Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
162NULL
163
164-- Iteration 12 --
165int(%d)
166
167-- Iteration 13 --
168
169Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
170NULL
171
172-- Iteration 14 --
173int(%d)
174
175-- Iteration 15 --
176
177Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
178NULL
179
180-- Iteration 16 --
181
182Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
183NULL
184
185-- Iteration 17 --
186
187Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
188NULL
189
190-- Iteration 18 --
191
192Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
193NULL
194
195-- Iteration 19 --
196
197Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
198NULL
199
200-- Iteration 20 --
201
202Warning: array_rand() expects parameter 2 to be long, object given in %s on line %d
203NULL
204
205-- Iteration 21 --
206
207Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
208NULL
209
210-- Iteration 22 --
211
212Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
213NULL
214Done
215
216