icon for lab

CT310

Recitation 6 - Fun with PHP

In this recitation, we will:


Server-side scripting language for web development

Step-by-Step example

Download the code

Download the file dependencies

Create a directory for this lab and download this file and rename it as "index.php" to get you started with the exercise.

  1. Printing

  2. Write a PHP script to print out the following string:

    • "Hello World!"
  3. Variables

  4. Now add to your script and declare a variable called game and append it to another echo statement that prints both the variable and the following:

    • "My favorite boardgame is [the value of game] "

    Instantiate game with the value of.. your favorite game! Feel free to replace that with your favorite videogame, movie, or music genre.

  5. Form Submission

  6. Copy this HTML code into your PHP file. This will make a small input submission box that can send data through the POST method in HTTP. Using PHP, you can access this with $_POST, which is a default PHP global variable that is treated as a key-value pair map.

    Update your script to take in input and display the user's favorite boardgame, videogame, music genre, movie, or whatever you chose.

  7. Array and If-Else statement

  8. Construct an array of 5 elements that answer your question above.

    Then, check if the value of game exists in the array. Display a message if it does and another if it does not exist.

  9. Array from File Contents

  10. View this data file and copy the text related to your question into a separate .txt file (for example, if you asked for a boardgame name, copy the 10 names under ~BOARDGAMES~).

    Open the file and construct an array from its contents. Try to use sort() in PHP to reorder the list in any way you want. Update your previous form submission to use the list from the file rather than your own list (you may add your other items into this list to make it longer if you wish). Finally, print out the array as an ordered list in HTML only after their value is submitted.