Playground Checkpoint
Fix the closed-Sunday bug in a week of slots and flatten the code — the expected output tells you when it's right.
The Challenge
Olivia wants the whole week on one page. The rules: Monday to Friday, 9:00 to 18:00; Saturday, 9:00 to 16:00; Sunday, closed. Every open day has a lunch break from 13:00 to 14:00, and slots are 30 minutes long.
Open the starter in the PHP Playground with the button below. It has the old closingHour() — a switch — and the old nested slotsFor(), plus a few lines that print the week. Run it: Sunday shows 16 slots, and the week total is wrong. Fix the bug, and rewrite both functions with what you learned in this Act.
What You Need to Do
- Run the starter and find why Sunday gets slots. Write down which line causes it (chapter 3).
- Rewrite
closingHour()as a singlematchexpression that returnsnullfor Sunday. Its return type becomes?int. - Rewrite
slotsFor()with a guard clause that returns an empty array on a closed day, and aforloop that skips the lunch break withcontinue(chapter 5). - Give the lunch-break condition a name, like
$isLunchBreak. - Inside each function, nest no more than two levels deep — the loop and one
ifinside it. - Don't change the lines under "Print the week". The output must match the expected output exactly, with no warnings.
Stuck? A Few Hints
- The Sunday case in the switch has no break, so it falls through into default (chapter 3).
- With match, Sunday can return null, and slotsFor() can start with: if ($closing === null) { return []; }.
- The lunch break is from 13:00 (780 minutes) up to, but not including, 14:00 (840 minutes).
- Check your numbers: 9:00 to 18:00 is 18 slots, minus 2 for lunch, is 16. Saturday is 14 minus 2, so 12.
Ready to Build It?
Opens the Playground, right in your browser — nothing to install.
A correct run prints eight lines: Monday to Friday each "16 slots, 9:00 to 17:30", then "Saturday: 12 slots, 9:00 to 15:30", "Sunday: closed" and "Week total: 92 slots" — and no warnings. The starter prints "Sunday: 16 slots, 9:00 to 17:30" and a total of 108.
Before You Move On
If your output matches, the website can no longer sell a Sunday slot — and if someone types a day name wrong, the match without a default fails loudly instead of opening the shop. Along the way you've used every part of PHP's control flow: if and elseif, all four loops, break and continue, switch and match, return and exit, and the guard clauses that keep it all flat. Next: strings — and a customer whose name gets cut in half in a text message.
