Types Checkpoint

Types Checkpoint

Playground Checkpoint

Predict ten mixed-type results, then add types so wrong values fail loudly instead of slipping through.

25–35 min

The Challenge

Marcus hands Mia a file with ten short lines of PHP and one untyped function. "Part one: write down what each line prints before you run it. Part two: make the function refuse bad values, then handle the partner's price properly."

Open the starter in the PHP Playground with the button below. Write your guess next to each var_dump() line first — honestly, without running it. Then run the file and compare. After that, follow the comments in part two.

What You Need to Do

  • Part 1: for each of the ten var_dump() lines, write your prediction in the comment before running the file. Then run it and mark which guesses were wrong. For every wrong one, write which chapter's rule explains it.
  • Part 2: add type declarations to bookingTotal() — both parameters and the return value — so that the call with "25 USD" stops with a TypeError instead of quietly giving 50. Run it and read the error message.
  • Then replace the bad call with a proper check: use filter_var() to turn $partnerPrice into an int, or reject it with the message shown in the expected output.
  • Compare $partnerPaid with ===, so the text "false" does not count as paid.
  • Keep declare(strict_types=1); as the first statement.
  • The final run must print exactly the expected output, with no warnings and no errors.
Stuck? A Few Hints
  • For Part 1: + needs numbers, . needs text, / gives a float unless the result is whole (chapters 2 and 7).
  • Floats are stored approximately; casts read only the number at the start of a string; only a few strings are false (chapter 7).
  • In PHP 8, comparing a number with a non-numeric string compares them as strings (chapter 7).
  • For Part 2: int parameters plus strict types turn "25 USD" into a TypeError (chapter 1). filter_var() returns the int or false — compare its result with === false (chapter 8).

Ready to Build It?

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

Open Playground

After the ten var_dump() results, a correct final run prints exactly: 5000 · Rejected price: "25 USD" · Paid: no — with no warnings and no errors. (The ten Part 1 results are in the sample solution — check them only after you've written your guesses.)

Before You Move On

If most of your guesses were right, PHP's type rules are no longer a mystery: you know which types exist, how to declare them — including "or", "nothing" and "never" — how PHP converts between them, and how to check outside data before trusting it. Wrong guesses are useful too: each one is a rule you'll now remember. Next: the operators that juggle all these types — including == and ===.