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', 16 0xabc => 2748, 0x12f => '303', 0xff => "255", 17 0123 => 83, 012 => 10, 010 => "8" 18); 19 20// Testing array_rand() function with various invalid 'req_num' values 21// with valid num_req values 22echo"\n-- With default num_req value --\n"; 23var_dump( array_rand($input) ); // with default $num_req value 24echo"\n-- With num_req = 1 --\n"; 25var_dump( array_rand($input, 1) ); // with valid $num_req value 26 27// with invalid num_req value 28echo"\n-- With num_req = 0 --\n"; 29try { 30 var_dump( array_rand($input, 0) ); // with $num_req=0 31} catch (\ValueError $e) { 32 echo $e->getMessage() . "\n"; 33} 34 35echo"\n-- With num_req = -1 --\n"; 36try { 37 var_dump( array_rand($input, -1) ); // with $num_req=-1 38} catch (\ValueError $e) { 39 echo $e->getMessage() . "\n"; 40} 41 42echo"\n-- With num_req = -2 --\n"; 43try { 44 var_dump( array_rand($input, -2) ); // with $num_req=-2 45} catch (\ValueError $e) { 46 echo $e->getMessage() . "\n"; 47} 48 49echo"\n-- With num_req more than number of members in 'input' array --\n"; 50try { 51 var_dump( array_rand($input, 13) ); // with $num_req=13 52} catch (\ValueError $e) { 53 echo $e->getMessage() . "\n"; 54} 55 56?> 57--EXPECTF-- 58*** Testing array_rand() : with invalid values for 'req_num' *** 59 60-- With default num_req value -- 61int(%d) 62 63-- With num_req = 1 -- 64int(%d) 65 66-- With num_req = 0 -- 67array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array) 68 69-- With num_req = -1 -- 70array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array) 71 72-- With num_req = -2 -- 73array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array) 74 75-- With num_req more than number of members in 'input' array -- 76array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array) 77