Operators Checkpoint

Operators Checkpoint

Playground Checkpoint

Rewrite the slot-availability check so it's correct and readable — five test cases tell you when it is.

20–30 min

The Challenge

Olivia's rule, word for word: "A slot can be booked if it's free, and the customer is a member or pays online, and the time is within opening hours — 9:00 to 18:00."

Open the starter in the PHP Playground with the button below. It contains canBook(), written in one long line with no brackets, and five test cases. Run it: two of the five answers are wrong. Rewrite the function so all five match, and so that Olivia could read it.

What You Need to Do

  • Run the starter first and find the two cases where the result doesn't match the expected answer. Write down, for each, how PHP grouped the line (chapter 3 and chapter 7).
  • Rewrite the body of canBook() so all five cases print the expected answer.
  • Split the rule into named bool variables — for example $canPay and $isDuringOpeningHours — and combine them in one short final line.
  • Use only &&, || and ! (no and/or), and strict comparisons where you compare values.
  • Use brackets wherever a reader might hesitate about the grouping.
  • Keep the test lines exactly as they are. The final output must match the expected output, with no warnings.
Stuck? A Few Hints
  • && is worked out before ||, so the starter's line groups as (free and member) or (online and hours). That's not Olivia's rule (chapter 3).
  • The opening-hours check needs two comparisons: from 9 (included) up to 18 (not included). Comparisons can't be chained — join them with && (chapter 2).
  • The payment part is the only "or" in the rule. Give it its own variable, and the rest becomes three ands.
  • Case 2 is Friday's double booking: a taken slot, an online payer.

Ready to Build It?

Opens the Playground, right in your browser — nothing to install.

Open Playground

A correct run prints five lines where every answer matches its expectation: Case 1: yes (expected yes) · Case 2: no (expected no) · Case 3: no (expected no) · Case 4: no (expected no) · Case 5: no (expected no) — and no warnings.

Before You Move On

If all five cases match, the line that caused Friday's double booking is now a rule anyone can read: three named facts joined by &&. Along the way you've met every PHP operator — arithmetic, comparison, logical, string, array, bitwise, the null and ternary family, and PHP 8.5's pipe — and the precedence rules that decide how they combine. Next: making decisions and repeating work with control flow — if, match and loops.