Create a New Dynamic Page Using PHP: Practice With if/elseif Statements and Request Variables

This is a free resource from my online course, From Idea To Launch, where I teach you how to build a full Laravel web application, step by step, at beginner's speed. Learn more →

To practice your new PHP skills, you’re going to build a new dynamic PHP-powered page to display information about a few different vehicles.


1. Create a new PHP page to display information about a few cars

Create a new vehicle.php page in your Code folder.

Add the basic HTML page code to this file. (Reference one of your other HTML/PHP page files for guidance.)

2. Create two PHP arrays at the top of your page, both named $vehicle

These arrays will each store information about a vehicle — the first one should contain information about a car, and then second should contain information about a truck. (Just pick a car and a truck, or make up some information.)

Each array should contain 3 pieces of information:

  1. make
  2. model
  3. year

3. Implement a type request variable

This type request variable should be set to either car or truck, designating whether to display the car’s information or the truck’s information.

Use PHP to retrieve the value of the type request variable.

4. Set the $vehicle array based on the value of type

Use an if/elseif statement to do this.

(And remember to create your empty array above the if/elseif to prevent errors in the case an erroneous type value is supplied! OR Get Bonus Points: You can include an else clause at the end of your if/elseif statement; use that to supply your empty array instead.)

5. Print out the vehicle information on the page

Use PHP echo statements to do this.

You should now have a new, dynamic PHP page!