PHP Random Quote

PHP Random Quote

We are searching data for your request:

Forums and discussions:
Manuals and reference books:
Data from registers:
Wait the end of the search in all databases.
Upon completion, a link will appear to access the found materials.

One way to add a random quote to your website is to use the PHP switch () statement. The switch statement compares one variable with many values and executes one piece of code depending on the value it equals. This allows you to prepare a list of quotations and then use the rand () function to select a number randomly that corresponds to one of the quotations.

Using Switch to Display Random Quotes

This example code demonstrates how to use switch () to display a random quote on your website. Each of the sample quotes is set to run when its number is chosen. By using rand () to choose a random number, one of the quotes is displayed at random. This example accommodates only six quotations, hence the rand (1,6) entry.


To add more quotes, you change the rand () function to allow for higher numbers and then add in their corresponding cases in the code.

If you want to include a random quote on your PHP web page, use include () to pull the quote from this file, like this:


Video, Sitemap-Video, Sitemap-Videos