Back to start

Exercises

These exercises are not mandatory for you to get a passing grade on the course. Instead they're an opportunity for you to test your sknowledge and skills in php and a way for you to make sure that you know what you need to know to follow along with the course.

Part 1: forms

Exercise 1

Create a form with method="post" and an <input type="text"> as well as an <input type="submit">. When you type a word into the input and submit the form the page should reload and show the number of characters in the word.

Exercise 2

Create a form like the one above but with 3 input-fields of type number. When the form is submitted the page should reload and show the sum of the numbers, the average, the lowest number, and the highest number.

Excercise 3

Use the built in $_SESSION variable to count the number of times the current visitor have reloaded the page.

Exercise 4

Create a form that posts two values (password and secret). When this form is submitted the values should be saved in the $_SESSION-variable.

Add another form that posts just the password. When this form is submitted you compare it to the password that was saved with the secret in the other form and if the passwords match you display the secret.

Part 2: Objects & classes

  1. Create a class called "Car" that contains the following members: make, model, year, and color.

  2. Create a constructor that accepts the parameters make, model, year, and color and sets the appropriate members.

  3. Create a method called "getDetails" that returns a string containing the car's make, model, year, and color.

  4. Create a class called "Driver" that contains the following members: name and license.

  5. Create a constructor that accepts the parameters name and license and sets the appropriate members.

  6. Create a method called "getLicense" that returns the driver's license number.

  7. Create a class called "CarDriver" that extends the Car and Driver classes.

  8. Create a constructor that calls the parent constructors and passes the necessary parameters.

  9. Create a method called "getCarDetails" that returns a string containing the car's make, model, year, color, and the driver's name and license number.

  10. Create an instance of the CarDriver class and call the "getCarDetails" method to see the results.

Part 3: Web API

Using the same database and table for Tasks that you created in Lab 1, create a web API that accepts JSON and returns JSON when applicable. The API should contain one file for each of the CRUD-operations, like this:

Create a separate file (called db.php or database.php) with all connection settings for the database and use require_once to load that file in each of your php-files.

Make sure to test all scripts by using postman to Create, Read, Update, and Delete tasks.