button that displays random array element

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 11
Joined: Sat Dec 29, 2012 6:02 pm

Tue Jan 01, 2013 9:35 pm

Code: Select all

$some_array = ["bob", "john", "billy", "susan", "sandy"]; // example array
$rand_num = rand(0,sizeof($some_array)-1); // generating random number between 0 and the length of the array
echo $some_array[$rand_num]; // displaying "a random element of the array"
Hi, the code above is pretty self-explanatory, it's just using the rand() function built in to php.

Hope this helped!

Post Reply