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