KefkaIIV Posted July 26, 2006 Share Posted July 26, 2006 [code]<?php/** * Add this line of code in your page: * <?php include "random_quote.php"; ?> */$quotes[] = "This is a quote";$quotes[] = "This is another";$quotes[] = "quote 3";$quotes[] = "quote 4";$quotes[] = "quote 5";$quotes[] = "quote 6";srand ((double) microtime() * 1000000);$randomquote = rand(0,count($quotes)-1);echo "<p>" . $quotes[$randomquote] . "</p>";?> [/code][b]Question 1:[/b] Could I change the echo statement to:[quote]echo "<p> $quotes[$randomquote] </p>";[/quote]If not, why? Do the periods have to be before and after the variables?Can I not just quote the whole thing, instead of only the "p"'s?[b]Question 2:[/b] What does this mean:[quote]srand ((double) microtime() * 1000000);[/quote]What does "srand" stand for, and what does the whole line of code do?[b]Question 3:[/b] How do I change the following so it displays more than one quote:[quote]$randomquote = rand(0,count($quotes)-1);[/quote]What would be changed, and why? Quote Link to comment https://forums.phpfreaks.com/topic/15717-randomizer/ Share on other sites More sharing options...
wildteen88 Posted July 26, 2006 Share Posted July 26, 2006 For yourfirst question. Yes you can do that, however some developers tend to do[code]echo "<p>" . $quotes[$randomquote] . "</p>";[/code]for readability when creating a script, Theres no need for it, as variables can be parsed within double quotes. However they can't with single quotes.secound question, that line isnt doing nothing. So theres no need for it to be there.third question. If you want to make it display more than one quote, either duplicate the line. Or put it in a for loop, and loop it a few times to get two or more results like so:[code]// get two results:for ($i = 0; $i < 2; $i++){ $randomquote = rand(0, count($quotes)-1); echo "<p>{$quotes[$randomquote]}</p>";}[/code]If you want to say display say 100 results, change the number 2 to 100, and the for loop will run the code 100 times producing 100 random results. Quote Link to comment https://forums.phpfreaks.com/topic/15717-randomizer/#findComment-64168 Share on other sites More sharing options...
KefkaIIV Posted July 27, 2006 Author Share Posted July 27, 2006 [quote author=wildteen88 link=topic=101957.msg404006#msg404006 date=1153932954]For yourfirst question. Yes you can do that, however some developers tend to do[code]echo "<p>" . $quotes[$randomquote] . "</p>";[/code]for readability when creating a script, Theres no need for it, as variables can be parsed within double quotes. However they can't with single quotes.[/quote]What are the periods before and after the variables? And what are they for?[quote]$randomquote = rand(0,count($quotes)-1);[/quote]Does "rand" stand for random? And why does it say "0, count".Why is it in parenthesis? And have a -1 at the end?Sorry, I am new, and trying to grasp all the concepts. Quote Link to comment https://forums.phpfreaks.com/topic/15717-randomizer/#findComment-64409 Share on other sites More sharing options...
KefkaIIV Posted July 27, 2006 Author Share Posted July 27, 2006 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/15717-randomizer/#findComment-64466 Share on other sites More sharing options...
AndyB Posted July 27, 2006 Share Posted July 27, 2006 All this stuff really is in the manual :)the "periods" are php's concatenation operator (adds strings together)$randomquote = rand(0, count($quotes)-1);rand() function - gets a random integer between two numbers0 - the first numbercount($quotes) - the number of elements in the array $quotes Quote Link to comment https://forums.phpfreaks.com/topic/15717-randomizer/#findComment-64487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.