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