Jump to content

Archived

This topic is now archived and is closed to further replies.

KefkaIIV

Randomizer...?

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.