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
Random Quote Script
Moderator: General Moderators
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)];
?>http://php.net/count
http://php.net/array_rand