exercises and solutions

Solution style guide

A solution should show one clear working answer and explain the decisions that matter. It should not only paste code or repeat the task.

For runnable PHP, keep expected output inside the same block. Explain edge cases immediately after the code. When several approaches are valid, lead with the clearest beginner-appropriate answer and briefly name the tradeoff.

Practical Example

PHP example
<?php

declare(strict_types=1);

$solution = [
    'steps' => ['validate input', 'calculate result', 'print output'],
    'shows_reasoning' => true,
];

echo implode(' -> ', $solution['steps']) . PHP_EOL;

// Prints:
// validate input -> calculate result -> print output

This outline is not enough by itself. A real solution must connect each important step to the exercise requirement and explain why its edge-case handling belongs there.

Practice

Improve A Thin Solution

Use array_search() to find the value.

Show solution

Use array_search($needle, $values, true) when types should match. It returns an integer or string key when it finds a value and false when it does not. Compare with === false, because key 0 is a valid match and would fail a loose truthiness check.