Jump to content

Randomizer...?


KefkaIIV

Recommended Posts

[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?
Link to comment
Share on other sites

For your

first 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.
Link to comment
Share on other sites

[quote author=wildteen88 link=topic=101957.msg404006#msg404006 date=1153932954]
For your

first 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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.