In this chapter
We'll learn how to ask PHP about a value's type while the code runs — gettype() and get_debug_type(), the is_ functions like is_int(), is_numeric() and filter_var() for text that should be a number, and instanceof for objects — and put it all together to fix the partner bug for good.
The Problem in Real Life
The last piece of the partner bug. Prices arrive as text, and some are fine — "25" — while some are not — "25 USD", "", "twenty". Mia needs to let the good ones through and reject the bad ones, loudly, before any calculation.
"Casting can't do it, because it never complains," says Marcus. "You need to ask about the value first. PHP has a whole family of functions for asking — and the one you'll trust most for numbers isn't the most obvious."
How do I ask PHP "is this a real whole number?" before I use it?
Mia
Guessing the Type vs. Asking for It
What is this value?
Data from outside can be any type — you need to find out before you use it.
Good text, bad text
"25" should pass; "25 USD", "" and "twenty" should be rejected.
Check at the door
One check where data enters, so the rest of the code can trust its types.
Checking Types at Runtime
Type checking at runtime: runtime means "while the code is running". Type declarations check types automatically at function calls. But often you need to check a value yourself, and decide what to do — use it, convert it, or reject it with a clear message. PHP gives you four tools:
- gettype and get_debug_type: "what type is this?" as text.
- The is_ functions: yes/no questions like
is_int()andis_string(). - is_numeric and filter_var: "is this text a proper number?"
- instanceof: "is this object of this class?"
gettype() and get_debug_type(): both give a value's type as a string. gettype() is old and uses old names: "integer", "double" (for float), "boolean", "NULL", and just "object" for every object. get_debug_type() (PHP 8) uses the same names as type declarations — int, float, bool, null — and gives an object's class name. Use get_debug_type(), especially in error messages, like "Expected int, got string". The table below compares them.
| Value | gettype() | get_debug_type() |
|---|---|---|
| 25 | integer | int |
| 12.5 | double | float |
| "25" | string | string |
| true | boolean | bool |
| null | NULL | null |
| [1] | array | array |
| new stdClass() | object | stdClass |
The is_ functions: for yes/no checks, PHP has one function per type: is_int(), is_float(), is_string(), is_bool(), is_array(), is_object(), is_null(), is_callable(), is_iterable() and more. Each returns true or false. They check the actual type, with no conversion: is_int("25") is false, because "25" is a string. That's exactly right for values inside your code — but for text from outside, you need the next tool.
<?phpvar_dump(is_int(25)); // bool(true)var_dump(is_int("25")); // bool(false): it's a stringvar_dump(is_string("25")); // bool(true)var_dump(is_float(12.5)); // bool(true)var_dump(is_bool(false)); // bool(true)var_dump(is_array([])); // bool(true)var_dump(is_null(null)); // bool(true)
is_numeric(): is_numeric() asks "is this a number, or a string that's fully a number?" — the numeric strings from chapter 7. "25", "12.5", "1e3" and " 25" pass; "25 USD", "" and "twenty" fail. It's useful, but loose for prices: it accepts decimals, negative numbers and scientific notation like "1e3".
<?phpvar_dump(is_numeric("25")); // bool(true)var_dump(is_numeric("12.5")); // bool(true)var_dump(is_numeric("1e3")); // bool(true): scientific notation countsvar_dump(is_numeric("25 USD")); // bool(false)var_dump(is_numeric("")); // bool(false)
filter_var(): for "is this text a proper whole number?", the most precise tool is filter_var($value, FILTER_VALIDATE_INT). It returns the int if the text is a valid whole number, and false if it isn't — so it checks and converts in one step, safely. Compare the result with === false (not ==, because a valid 0 would also be falsy — chapter 7). You can even give it a range, like "between 0 and 100000". There's a matching FILTER_VALIDATE_FLOAT, and Act 20 uses filter_var() for all kinds of input.
| Text | is_int() | is_numeric() | FILTER_VALIDATE_INT |
|---|---|---|---|
| "25" | false | true | 25 |
| "12.5" | false | true | false |
| "1e3" | false | true | false |
| "25 USD" | false | false | false |
| "" | false | false | false |
| "twenty" | false | false | false |
is_int() is false for all of them — they're strings. Use it for values already inside your code.
<?phpvar_dump(filter_var("25", FILTER_VALIDATE_INT)); // int(25): checked AND convertedvar_dump(filter_var("25 USD", FILTER_VALIDATE_INT)); // bool(false)var_dump(filter_var("12.5", FILTER_VALIDATE_INT)); // bool(false): not whole// With a range: a price between 0 and 100000 pence$options = ["options" => ["min_range" => 0, "max_range" => 100000]];var_dump(filter_var("-500", FILTER_VALIDATE_INT, $options)); // bool(false)
instanceof: for objects, $value instanceof Booking asks "is this an object of the Booking class — or of a class that extends it?" (chapter 6). It works with interfaces too: $items instanceof Countable. It's the object version of the is_ functions, and Act 05 shows it again with the other operators.
<?php$items = new ArrayObject(["9:00", "9:30"]);var_dump($items instanceof ArrayObject); // bool(true)var_dump($items instanceof Countable); // bool(true): it can be countedvar_dump($items instanceof stdClass); // bool(false)
Check at the door: put it all together, and you get the rule that fixes the partner bug for good. Check outside data once, where it enters your code — and turn it into the right type there. After that door, everything is properly typed, typed functions pass it along, and strict types catches any mistake. The code below is Mia's final version: bad prices are rejected with a clear message, good ones become ints, and the paid flag is compared exactly.
<?phpdeclare(strict_types=1);function bookingTotal(int $pricePence, int $people): int{return $pricePence * $people;}// From the partner: everything is text$partnerPrice = "25 USD";$partnerPaid = "false";// The door: check and convert once$pricePence = filter_var($partnerPrice, FILTER_VALIDATE_INT);if ($pricePence === false) {echo "Rejected: price \"$partnerPrice\" is not a whole number (got ", get_debug_type($partnerPrice), ")\n";} else {echo "Total: ", bookingTotal($pricePence, 2), " pence\n";}$isPaid = $partnerPaid === "true"; // exact comparison, no jugglingvar_dump($isPaid); // bool(false)
Output: Rejected: price "25 USD" is not a whole number (got string), then bool(false). Change the price to "2500" and it prints Total: 5000 pence.
Key Takeaway
To check a type yourself while code runs: get_debug_type() names it (prefer it over the older gettype()), is_int()/is_string()/… answer yes or no without converting, is_numeric() accepts any numeric string, filter_var($v, FILTER_VALIDATE_INT) checks and converts whole numbers safely (compare with === false), and instanceof checks objects. Check outside data once, at the door, then trust your types.
Why This Matters
Every value from outside — forms, links, partner sites, files — has to pass through a door like this one. With these checks, Mia can reject bad data with a clear message instead of guessing, and everything after the door can rely on its types. That's the foundation of the validation work in Acts 16, 17 and 20.
And it closes the Act: types declared on every function, strict_types on, === for comparisons, outside data checked once at the door. That's how a price of "25 USD" will never again become a booking total at BookBay.
The partner bookings now come through cleanly: good prices become ints, bad ones are rejected with a message the partner can act on, and nobody gets a free haircut. Olivia is happy. Marcus isn't quite finished. "One more test," he says, and hands Mia ten lines of PHP. "Predict every result before you run it." That's the checkpoint.
