PHP Reference ยท Types and Type Handling
PHP declare(strict_types=1)
Reference for declare(strict_types=1): what strict typing changes for scalar arguments and return values, where to place it, and its limits.
By default PHP converts scalar arguments to match a function's type declarations: passing "5" to an int parameter works. The directive declare(strict_types=1); turns this off for the file that contains it. In strict mode, a scalar argument must already have exactly the declared type, with one exception: an int may be passed where a float is expected.
The directive affects calls made from the file that declares it, and return statements inside that file. It does not change how other files behave, and it does not change casts or ordinary operators.
<?php declare(strict_types=1); function double(int $n): int { return $n * 2; } double("5"); // TypeError in strict mode
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
<?php declare(strict_types=1);
| Rule | Detail |
|---|---|
| Placement | Must be the very first statement of the file (only a <?php tag, comments or whitespace before it). Anywhere else is a fatal error. |
| Scope | One file at a time. It cannot be set with ini_set(), per directory, or inside include for the included file. |
| Applies to | Scalar type declarations (int, float, string, bool) on arguments of calls made in the file, on return values of functions defined in the file, and on built-in function calls made from it. |
| Allowed widening | int to float. |
| Unaffected | Casts, arithmetic, string concatenation, comparisons, and null/array/object checks (those are always strict). |
Usage notes
- Weak mode and fractions. Passing
5.5to anintparameter without strict types works but emits a deprecation ("Implicit conversion from float 5.5 to int loses precision") since PHP 8.1; strict mode throws aTypeError. - The caller decides. A function defined in a strict file but called from a weak file receives coerced arguments. A weak function called from a strict file gets strict checks.
- Return types follow the file where the function is defined.
- Internal functions obey the mode of the calling file, so
strlen(123)fails in strict mode. - Without the directive, passing
nullto a non-nullable parameter of a user function is always aTypeError; for internal functions it is deprecated in weak mode (8.1) and an error in strict mode. - Extra safety, little cost. Most modern code bases and framework generators put
declare(strict_types=1);at the top of every file. - Union types: in strict mode a value must match one of the members exactly (plus
inttofloat). - Property types are also checked strictly in a strict file when the assignment happens there.
- Not a security feature. It prevents silent conversions, but you still need validation of user input; input arrives as strings and has to be converted deliberately.
- Related directives:
ticksandencoding. See declare.