Home and Learn: PHP Programming Course
You can grab a random key from an array. This could be useful in games of chance. Here's a simple script that simulates a single dice throw:
$numbers = array(1 => 1, 2 => 2, 3 =>
3, 4 => 4, 5 => 5, 6 => 6);
$random_key = array_rand($numbers, 1);
The function that returns the random key is this:
You start off with the function array_rand( ). In between the round brackets, you need two things: the name of your array, and how many random keys you want to grab.
Try the script out. Refresh the page and you should see a different number
between 1 and 6 display.
Email us: enquiry at homeandlearn.co.uk