Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.
Routine (15–30 minutes)
Before the recap and quiz, consolidate with a 15–30 minute routine:
- Write a script that builds an array of items, loops with
foreach, and prints a small report with string concatenation. - Add a
functionthat takes an int and returns a grade string usingmatchorif. - Add a second function that validates a simple rule (e.g. non-empty string) and returns bool.
Habits
Run php -l before every run. If something surprises you, var_dump intermediate values.
This mirrors how you will work in small apps: data → logic → output.
The code checker below is a guided slice of this routine: fixed scores, a foreach report line, and gradeForScore()—so you get fast feedback before the chapter quiz.
Exercise
Complete the starter: implement gradeForScore(int $score): string (A if score ≥ 90, B if ≥ 80, else C), then foreach over $items and print Name: grade on its own line—Ada first, then Lin—to match the expected output exactly.
From Zero to PHP — Control flow, functions, arrays, and the web