Page 1 of 1

Random Quote Script

Posted: Wed May 28, 2003 3:02 pm
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

Posted: Wed May 28, 2003 3:15 pm
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

Thanks

Posted: Wed May 28, 2003 3:18 pm
by p3pC
Thanks, that's exactly what i was after.



p3pC