1--TEST--
2Test array_rand() function : usage variation - invalid values for 'req_num' 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 behaviour of array_rand() function when associative array and
12* various invalid values are passed to the 'input' and 'req_num'
13* parameters respectively
14*/
15
16echo "*** Testing array_rand() : with invalid values for 'req_num' ***\n";
17
18// initialise associative arrays
19$input = array(
20  1 => 'one', 2.2 => 'float key', 0.9 => 'decimal key',
21  2e2 => 'exp key1', 2000e-3 => 'negative exp key',
22  0xabc => 2748, 0x12f => '303', 0xff => "255",
23  0123 => 83, 0129 => 10, 010 => "8"
24);
25
26// Testing array_rand() function with various invalid 'req_num' values
27// with valid num_req values
28echo"\n-- With default num_req value --\n";
29var_dump( array_rand($input) );  // with default $num_req value
30echo"\n-- With num_req = 1 --\n";
31var_dump( array_rand($input, 1) );  // with valid $num_req value
32
33// with invalid num_req value
34echo"\n-- With num_req = 0 --\n";
35var_dump( array_rand($input, 0) );  // with $num_req=0
36echo"\n-- With num_req = -1 --\n";
37var_dump( array_rand($input, -1) );  // with $num_req=-1
38echo"\n-- With num_req = -2 --\n";
39var_dump( array_rand($input, -2) );  // with $num_req=-2
40echo"\n-- With num_req more than number of members in 'input' array --\n";
41var_dump( array_rand($input, 13) );  // with $num_req=13
42
43
44echo "Done";
45?>
46--EXPECTF--
47*** Testing array_rand() : with invalid values for 'req_num' ***
48
49-- With default num_req value --
50int(%d)
51
52-- With num_req = 1 --
53int(%d)
54
55-- With num_req = 0 --
56
57Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
58NULL
59
60-- With num_req = -1 --
61
62Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
63NULL
64
65-- With num_req = -2 --
66
67Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
68NULL
69
70-- With num_req more than number of members in 'input' array --
71
72Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
73NULL
74Done
75
76