Jump to content

random quotes from a textfile


ill-fated24

Recommended Posts

new in php..

if you would not mind..

need some explanation here.. :(

 

i manage to get and post random quotes from a textfile(quotes.txt).. .the codes are from the net..so i want to know how it works instead of wondering how,,

 

<?php
$quotes = "quotes.txt";
$quotes = file($quotes);
srand((double)microtime() * 1000000);
$ranNum = rand(0, count($quotes)-1);

echo ($quotes[$ranNum]);
?>

 

anyone can explain this line by line?

i would be very thankful..thanks

Link to comment
Share on other sites

<?php
$quotes = "quotes.txt";

I assume you understand adding value to variables. this is the name of the file

$quotes = file($quotes);

This opens the file quotes.txt and creates an array of every line. IE if you had 10 lines, the variable

$quotes[0] would be the first line, and so on

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

this is called seeding the rand function. basically it tells the rand function where to start I think, or something like that. however, since PHP version 4.2.0 there is no need to do this as it is done automatically when you call the rand function

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

this calls the rand function itself. it takes a random number between 0, and the maximum of the quotes array. (IE if there are 27 lines, the max would be 27) However, since arrays are zero based, the first entry starts at 0, so you need to subtract 1 from the count (IE if the file had five lines, array[0] would be first, array[1] is second and so on)

echo ($quotes[$ranNum]);
?>

this echos a random index of the $quotes array.

 

hope that explained it well enough

Link to comment
Share on other sites

// Specify the name of the file containing the quotes to be loaded
$quotes = "quotes.txt";

/* Load the entire file into an array.  This creates an array with each element 
containing one line from the file.  A line ends with a new-line and the new-line
is kept on the end of the line in the array.  The array will be indexed starting at zero. */
$quotes = file($quotes);

/* seed the random number generator so we get a more random random number */
srand((double)microtime() * 1000000);

/* count($quotes) returns the number of entries in the $quotes array.  This is the 
number of lines loaded from the file.  rand() returns a random number between the
two values supplied (in this case zero and count()-1).  We have to subtract 1 from
the count because the index starts at zero and therefore the maximum is one less 
than the count. */
$ranNum = rand(0, count($quotes)-1);

// Print the Nth line from the quote file (where N is the random number we just generated)
echo ($quotes[$ranNum]);

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.