Skip to content

PHP Basics Offline Exercise Pack

Practise four exercises from PHP Basics offline. You need PHP 8.0 or later on your machine. Use the PHP setup lesson if php -v is unavailable.

Download the PHP Basics exercise pack (ZIP)

  1. Extract the ZIP and open the php-basics-exercises folder in a terminal.
  2. Read the matching section in README.md for the instructions and expected values.
  3. Open a starter script in your editor. For the first exercise, edit starters/01-calculations.php and run php starters/01-calculations.php.
  4. When the values match, try different inputs. Compare your approach with the corresponding script in solutions/.
ExerciseCheck your resultRead the lesson
Calculate with two numbersFor 20 and 5, the sum is 25 and the quotient is 4.First PHP script
Print FizzBuzz from 1 to 100Item 15 is FizzBuzz and item 100 is Buzz.Control structures
Convert temperatures with functions25 °C equals 77 °F and 298.15 K.Functions
Sort a grade report by averageCharlie ranks first with 95.00. Eve is last with 80.40.Arrays

Run these from the extracted folder:

Terminal window
php solutions/03-fizzbuzz.php
php solutions/03-fizzbuzz-match.php

The chapter solution uses an if chain for the first 100 items, then demonstrates match for 50. The alternate file uses match for all 100. Both check the combined condition before testing individual factors. An if chain leaves room for several actions in each branch. A match expression fits here when the only decision is which label to return.

Terminal window
php solutions/06-student-grades.php
php solutions/06-student-grades-array-map.php

The chapter solution builds a student-to-average array with foreach. The alternate applies array_map to a single associative array so each calculated average keeps its original student key. Use the loop if you want to inspect each row or apply different rules per student. The map is short when every row follows the same formula.

If you’d rather fix an existing script, try the PHP debugging challenges.