PHP Code Snippet: Quote of the Day Picker

/ PHP / by Paul Robinson / 0 comments
This post was published back on April 18, 2012 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

This code snippet will pick a random quote from an array of quotes you provide. It will show that same quote for the whole day and change to a new one the next day. This code works fairly well, but it relies on seeding. This means that it is entirely possible that the same quote will be picked on consecutive days. The more quotes you have the less likely that is to happen, but it is still possible. I’ll talk a little later about how to get around that problem.

Let’s take a look the code.

To be honest it’s wonderfully simple. First we have an array of quotes. This array is multi-dimensional so that we can hold both the quote & the author.

Next we get the highest key in the array. We could use (count($quotes) - 1) but I find it’s best to use this method. The next two lines generate a pseudo random number. A pseudo random number is generated via a seed, in this case the current date (YYYY-MM-DD format). The date changes each day and because the seed generates our random number, our random number will only change when the date changes. We then grab our random number between 0 and our highest key ($total). Finally we return our quote.

Usage of the function would be something like this:

It is important to note that due to the nature of pseudo random number generators perceived randomness is largely determined by the amount of items you have. The more quotes you have in your array the less likely you are to receive a repeat quote on a consecutive day.


Earlier I said I would explain how to stop repeat quotes. There is only 1 way I know to stop repeating and that is by storing the quotes in a database. You would pull quotes back from a database and once that quote has been picked you would mark a boolean field so that you can tell that quote has been picked. This is outside the scope of this little article, but I thought it was worth mentioning for those looking for what I like to call intelligent randomness.

For those wondering ‘Intelligent Randomness’ is what I like to call the thought that randomly picked items should not repeat. In truth it is entirely possible for a repeat to be picked when generating a random number however the vast majority of people find this un-natural.

That’s about it. This little snippet is extremely useful as a Quote of the Day picker, but also for other situations where you require a random item to be picked daily. All it requires is an array as input and that the array it is handed has consecutive keys. If you have an array you’d like to use but the keys are not consecutive just throw it through the array_values() function like so:

That will reorder the keys so they start from 0 and are consecutive. Cool, huh!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more