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)
Work through the exercises
Section titled “Work through the exercises”- Extract the ZIP and open the
php-basics-exercisesfolder in a terminal. - Read the matching section in
README.mdfor the instructions and expected values. - Open a starter script in your editor. For the first exercise, edit
starters/01-calculations.phpand runphp starters/01-calculations.php. - When the values match, try different inputs. Compare your approach with the corresponding script in
solutions/.
| Exercise | Check your result | Read the lesson |
|---|---|---|
| Calculate with two numbers | For 20 and 5, the sum is 25 and the quotient is 4. | First PHP script |
| Print FizzBuzz from 1 to 100 | Item 15 is FizzBuzz and item 100 is Buzz. | Control structures |
| Convert temperatures with functions | 25 °C equals 77 °F and 298.15 K. | Functions |
| Sort a grade report by average | Charlie ranks first with 95.00. Eve is last with 80.40. | Arrays |
Compare two working approaches
Section titled “Compare two working approaches”Choose a FizzBuzz label
Section titled “Choose a FizzBuzz label”Run these from the extracted folder:
php solutions/03-fizzbuzz.phpphp solutions/03-fizzbuzz-match.phpThe 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.
Average a grade list
Section titled “Average a grade list”php solutions/06-student-grades.phpphp solutions/06-student-grades-array-map.phpThe 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.