exercises and solutions

Exercise style guide

Exercises should prove a skill that the course already taught. A useful prompt names the input, the required behaviour, an edge case, and a visible result. It should feel like a small PHP task that another developer could review in one pull request.

Avoid vague prompts such as “practise arrays” or puzzles that reward guessing. Prefer a realistic action: calculate a basket total, validate a submitted value, filter records, prepare a safe query, or review a vulnerable output path.

Practical Example

PHP example
<?php

declare(strict_types=1);

$exercise = [
    'goal' => 'Calculate a basket total',
    'starter_code' => true,
    'expected_output' => 'Total: 1250',
];

foreach ($exercise as $field => $value) {
    echo $field . ': ' . (is_bool($value) ? ($value ? 'yes' : 'no') : $value) . PHP_EOL;
}

// Prints the exercise structure fields.

This structure is useful only when the prompt also states the actual input and required edge cases. Starter code should remove setup noise without solving the skill being tested.

Practice

Rewrite A Weak Array Exercise

Write some PHP that demonstrates arrays.

Include realistic input, required operations, one edge case, and a visible result.

Show solution

Given an array of products with name, price_pence, and active keys, keep only active products and print each product name with its price. Include an empty-array case.

This proves lookup, filtering, iteration, formatting, and edge-case handling. A reviewer can tell whether the answer satisfies the task.