Generate Random Quotes with PHP
The following will show you how to generate simple random quotes using PHP.
The first step it to open the PHP script:
<?
Next we will need to open the array sequence.
$quote = array(
You can have as many as you want, just open each one as a new number:
1 => "a", 2 => "b", 3 => "c", 4 => "d", 5 => "e",
Now close the array:
);
Now set up the random sequence. Notice that rand (1,5) meaning 1 through 5. you could change this to be 3,5 and it would only use array 3, 4 and 5:
srand ((double) microtime() * 1000000); $randnum = rand(1,5);
Notice that the array uses $quote, and the random sequence uses $randnum, so we need to bind those two together:
echo”$quote[$randnum]“;
Now end the PHP script:
?>
Here is the complete code:
<? $quote = array( 1 => "a", 2 => "b", 3 => "c", 4 => "d", 5 => "e", ); srand ((double) microtime() * 1000000); $randnum = rand(1,5); echo"$quote[$randnum]"; ?>
If you found this post useful you may also want to check these out:
