In this chapter
We'll learn what a type system is, the two questions that describe any language's types — when are types checked, and does the language convert values for you — where PHP stands on both, and why strict_types didn't stop a price of "25 USD" from turning into a booking total.
The Problem in Real Life
BookBay has its first partner: a website that sells haircut deals and sends the bookings on to Tom. The partner's data arrives as text. One booking says the price is "25 USD", and it's paid: "false".
Mia's code multiplies the price by the number of people and gets 50 — with only a warning in the log, A non-numeric value encountered. Then it checks if ($paid == true), and the booking is confirmed. Nobody had paid. The text "false" counted as true.
"But every file starts with declare(strict_types=1)!" says Mia. Marcus nods. "Strict types guards the doors of your functions. These values never went through a door. To understand why, you need to see PHP's whole type system."
We switched on strict types in Act 02. How did a price like "25 USD" still get through?
Mia
What the Data Was vs. What PHP Assumed
Data arrives as text
Prices and yes/no values from outside often come as strings, like "25 USD" or "false".
PHP converts quietly
In calculations and == checks, PHP turns values into other types to make them fit.
Strict types isn't everywhere
declare(strict_types=1) checks function calls and returns — not every line of code.
PHP's Type System: Dynamic, Strict and Weak Typing
Type system: as you know from Act 02, every value has a type — whole number, text, true/false and so on. A language's type system is its set of rules about types: which types exist, which values belong to which type, and what happens when values of different types meet — for example, when you multiply a piece of text by a number. Every type system can be described by answering two questions:
- When are types checked: before the program runs, or while it runs?
- Does the language convert values for you: when a value has the wrong type, does it quietly turn it into the right one, or refuse?
Static vs. dynamic typing: in a statically typed language, like Java or C#, every variable has a fixed type, and a tool called a compiler checks all the types before the program runs — a wrong type means the program doesn't start at all. In a dynamically typed language, like PHP, JavaScript or Python, types are checked while the program runs, line by line. You saw this in Act 03: a PHP variable has no fixed type; its value does, and the same variable can hold a number now and text later. So PHP is dynamically typed.
Weak vs. strong typing: the second question is about converting. A weakly typed language converts values automatically to make an operation work. A strongly typed language refuses and reports an error. By default, PHP is weakly typed: give it the text "10" where a number is needed, and it uses the number 10. This automatic converting is called type juggling (chapter 7 covers the details). It's often handy — and it's exactly how "25 USD" became 25 and "false" became true.
<?phpvar_dump("10" + 5); // int(15): the text "10" was used as a numbervar_dump("10" . 5); // string(3) "105": the number 5 was used as text
PHP lets you choose: modern PHP gives you tools to make your code much stricter, step by step. Type declarations (Act 02, chapter 6) say which types a function accepts and returns. And declare(strict_types=1); stops PHP from converting values at those points. Adding types where you want them, instead of everywhere at once, is sometimes called gradual typing. The table below compares PHP with a few other languages.
| Language | When are types checked? | Converts values for you? |
|---|---|---|
| PHP, default | While running | Yes — often |
| PHP, typed functions + strict_types | While running, at function calls and returns | No, at those points |
| JavaScript | While running | Yes — often |
| Python | While running | Rarely |
| Java, C# | Before running (compiler) | Rarely |
Where type declarations can go: you can declare a type in four places: a function's parameters, a function's return value, a class's properties and a class's constants (classes come in Act 11). Variables and plain lines of code have no type declarations — there is no way to write "$price must always be an int".
What strict_types does — and doesn't — cover: this answers Mia's question. strict_types only works at those declared places: when a value goes into a typed function, or comes out of one, PHP checks it. A calculation like $price * 2 isn't a function call, so it still juggles — even with strict types on. That's why the code below gives 50 and only a warning. The fix isn't more strict types; it's making sure values have the right type before you use them, and passing them through typed functions. In the second code box, the same price goes into a function with int parameters — and PHP stops it with a clear TypeError.
<?phpdeclare(strict_types=1);$price = "25 USD"; // from the partnervar_dump($price * 2);// Warning: A non-numeric value encountered in main.php on line 6// int(50)
<?phpdeclare(strict_types=1);function bookingTotal(int $price, int $people): int{return $price * $people;}echo bookingTotal("25 USD", 2);// Fatal error: Uncaught TypeError: bookingTotal(): Argument #1 ($price)// must be of type int, string given
Now the wrong value can't slip through: PHP stops at the function's door and says exactly what's wrong.
Static checking on top: PHP itself checks types while the code runs. But tools like PHPStan (Act 21) read your code without running it and check every declared type, like a compiler would. With good type declarations, they catch most of this Act's bugs before anyone opens the page. That's one more reason to declare types everywhere: they're checked by PHP, read by tools, and understood by every developer.
Key Takeaway
PHP is dynamically typed (types are checked while the code runs) and, by default, weakly typed (it converts values to make operations work — type juggling). Type declarations plus declare(strict_types=1) make function boundaries strict, but ordinary calculations and == checks still juggle. So check and convert outside data first, then pass it through typed functions.
Why This Matters
BookBay will soon take data from forms, partner websites and databases — and almost all of it arrives as text. Knowing where PHP converts values quietly, and where it checks them strictly, tells Mia exactly where bugs like a £50 unpaid booking can hide.
This chapter is also the map for the rest of the Act: the types themselves (chapters 2–6), how PHP converts between them (chapter 7), and how to check a value's type yourself (chapter 8).
Marcus writes two lists on the whiteboard: "Things that can be types" and "Things PHP converts". The first list starts with four words Mia has seen since Act 02 — bool, int, float, string — and she realises she doesn't really know them. Why does 7 / 2 give 3.5, but 6 / 2 gives 3? And why does 0.1 + 0.2 not equal 0.3? That's the next chapter.
