Playground Checkpoint
Fix the shared price functions so all six files show the same, correct price — the expected output tells you when it's right.
The Challenge
Olivia's rule for launch: one price, everywhere. All six files — booking, checkout, reminder, invoice, admin and the app's API — must call the same functions. The prices: a haircut is £25.00, a beard trim £10.00, a hot towel £2.50. On a bank holiday, 10% is added to the whole booking, rounded down to the penny.
Open the starter in the PHP Playground with the button below. It has the shared functions as Mia first wrote them, and the six calls the files make. Run it: the prices come out in the wrong place, the add-ons are missing, the holiday surcharge is missing, and there's a warning. Fix the functions with what you learned in this Act.
What You Need to Do
- Run the starter and find each problem in the output. For each one, note which chapter explains it.
- Make
formatPrice()return the text instead of printing it, with astringreturn type (chapter 1). - Make
addOnsTotal()actually add up the add-ons — an arrow function can't change an outer variable (chapter 7). - Make
holidaySurcharge()get its rate without reading a variable from outside the function: a constant, or a parameter with a default (chapter 5). - Keep every function typed, and keep the money in pence (
int) untilformatPrice()(chapter 2). - Don't change the lines under "What each of the six files asks for". The output must match the expected output exactly, with no warnings.
Stuck? A Few Hints
- formatPrice() echoes and returns null, so the price is printed before the file name. return the string instead (chapter 1).
- fn($addOn) => $sum += ... only changes the arrow function's copy of $sum. array_sum(array_map(...)) needs no outer variable at all (chapter 7).
- Inside holidaySurcharge(), $surchargeRate doesn't exist. const HOLIDAY_SURCHARGE_PERCENT = 10; is visible everywhere (chapter 5).
- Check one line by hand: haircut 2500 + hot towel 250 = 2750; 10% rounded down is 275; 2750 + 275 = 3025, which is £30.25.
Ready to Build It?
Opens the Playground, right in your browser — nothing to install.
A correct run prints six lines, one per file: book.php £25.00, checkout.php £30.25, reminder.php £11.00, invoice.php £37.50, admin.php £13.75, api.php £27.50 — each price after its file name, and no warnings. The starter prints every price before its file name, ignores the add-ons and the holiday surcharge, and warns about an undefined variable.
Before You Move On
If your output matches, every file of BookBay now shows the same price, and the next rule change is a change in one place. Along the way you've used the whole of PHP's function toolbox: declarations and return values, types, defaults, named and variadic arguments, value vs. reference, scope and static, recursion, closures and arrow functions, first-class callables, higher-order functions, built-ins, never and generators. Next: what really happens when PHP runs your code — scope, execution and the runtime.
