Jump to content


Photo

Randomizer...?


  • Please log in to reply
4 replies to this topic

#1 KefkaIIV

KefkaIIV
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 26 July 2006 - 04:41 PM

<?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>";

?>

Question 1: Could I change the echo statement to:

echo "<p> $quotes[$randomquote] </p>";


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?

Question 2: What does this mean:

srand ((double) microtime() * 1000000);


What does "srand" stand for, and what does the whole line of code do?

Question 3: How do I change the following so it displays more than one quote:

$randomquote = rand(0,count($quotes)-1);


What would be changed, and why?

Click the below banner to earn money for participating in surveys!


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 04:55 PM

For your

first question. Yes you can do that, however some developers tend to do
echo "<p>" . $quotes[$randomquote] . "</p>";
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:
// get two results:
for ($i = 0; $i < 2; $i++)
{
    $randomquote = rand(0, count($quotes)-1);
    echo "<p>{$quotes[$randomquote]}</p>";
}
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.

#3 KefkaIIV

KefkaIIV
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 27 July 2006 - 12:21 AM

For your

first question. Yes you can do that, however some developers tend to do

echo "<p>" . $quotes[$randomquote] . "</p>";
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.


What are the periods before and after the variables? And what are they for?

$randomquote = rand(0,count($quotes)-1);


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.

Click the below banner to earn money for participating in surveys!


#4 KefkaIIV

KefkaIIV
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 27 July 2006 - 02:53 AM

Anyone?

Click the below banner to earn money for participating in surveys!


#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 27 July 2006 - 04:06 AM

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 numbers

0 - the first number
count($quotes) - the number of elements in the array $quotes
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users