General discussions related to php
Moderators: macek, egami, gesf
by thelord104 » Mon Nov 26, 2012 7:41 am
i need a button that when pressed will go into my array and display a random element of it. any help would be much appreciated
-
thelord104
- New php-forum User

-
- Posts: 1
- Joined: Mon Nov 26, 2012 7:36 am
by Snaek » Tue Jan 01, 2013 9:35 pm
- Code: Select all
<?php
$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!
-
Snaek
- New php-forum User

-
- Posts: 11
- Joined: Sat Dec 29, 2012 6:02 pm
Return to PHP General
Who is online
Users browsing this forum: No registered users and 1 guest