Random Quote Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
p3pC
Forum Newbie
Posts: 2
Joined: Wed May 28, 2003 3:02 pm

Random Quote Script

Post by p3pC »

Hi,

I'm new to PHP, having a go at making a random quote script. I'm using an array to store the quotes and want to use rand function to pull them out, but I need to know how many elements there are in the array.

Can anyone help me? Is there a function to count array elements?


Thanks

p3pC
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
srand ((double)microtime()*1000000); // for php versions prior 4.2
$arr = array('a', 'b', 'c', 'd', 'e');

// either
echo $arr[rand(0, count($arr)-1)];
echo "\n";
// or
echo $arr[array_rand($arr)];
?>
where do those array elements come from?


http://php.net/count
http://php.net/array_rand
p3pC
Forum Newbie
Posts: 2
Joined: Wed May 28, 2003 3:02 pm

Thanks

Post by p3pC »

Thanks, that's exactly what i was after.



p3pC
Post Reply