ill-fated24 Posted September 12, 2009 Share Posted September 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173972-random-quotes-from-a-textfile/ Share on other sites More sharing options...
mikesta707 Posted September 12, 2009 Share Posted September 12, 2009 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/173972-random-quotes-from-a-textfile/#findComment-917065 Share on other sites More sharing options...
DavidAM Posted September 12, 2009 Share Posted September 12, 2009 // 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]); Quote Link to comment https://forums.phpfreaks.com/topic/173972-random-quotes-from-a-textfile/#findComment-917066 Share on other sites More sharing options...
ill-fated24 Posted September 12, 2009 Author Share Posted September 12, 2009 thanks for the quick replies.. very much appreciated,,, Quote Link to comment https://forums.phpfreaks.com/topic/173972-random-quotes-from-a-textfile/#findComment-917068 Share on other sites More sharing options...
ill-fated24 Posted September 12, 2009 Author Share Posted September 12, 2009 how about if i want to lets say explode the file/quotes with " * " instead of by line? Quote Link to comment https://forums.phpfreaks.com/topic/173972-random-quotes-from-a-textfile/#findComment-917072 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.