Sunday, March 8, 2020

Using PHP to Display Random Quotes

Using PHP to Display Random Quotes 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: INCLUDE yoursite.com/path/to/quote_file.php