Skip to main content

How to Turn On Strict Types in PHP with declare(strict_types=1)

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

By default, PHP converts a value to the type a function asks for when it can: pass the string "21" where an int is expected and PHP quietly makes it 21. One line at the top of a file, declare(strict_types=1);, turns that off, so a wrong type stops the call with an error.

Syntax​

Syntax
<?php

declare(strict_types=1);
PartWhat it does
declare(strict_types=1);Turns on strict type checking for calls made from this file.
PositionIt must be the first statement in the file, right after <?php.

The PHP manual on type declarations: "By default, PHP will coerce values of the wrong type into the expected scalar type declaration if possible." And: "It is possible to enable strict mode on a per-file basis. In strict mode, only a value corresponding exactly to the type declaration will be accepted, otherwise a TypeError will be thrown. The only exception to this rule is that an int value will pass a float type declaration."

The Example​

The example turns on strict types and calls a function that expects an int, first with 21 and then with the string "21".

Prerequisites​

  • PHP for the command line. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which declares strict_types=1, defines double(int $n): int returning n times 2, and echoes double(21) and double("21").

main.php
<?php

declare(strict_types=1);

function double(int $n): int
{
return $n * 2;
}

echo double(21), "\n";
echo double("21"), "\n";

Step 2: Run It​

Terminal
php main.php
Output
42
PHP Fatal error: Uncaught TypeError: double(): Argument #1 ($n) must be of type int, string given, called in /Users/Shared/you/main.php on line 11 and defined in /Users/Shared/you/main.php:5
Stack trace:
#0 /Users/Shared/you/main.php(11): double('21')
#1 {main}
thrown in /Users/Shared/you/main.php on line 5

Fatal error: Uncaught TypeError: double(): Argument #1 ($n) must be of type int, string given, called in /Users/Shared/you/main.php on line 11 and defined in /Users/Shared/you/main.php:5
Stack trace:
#0 /Users/Shared/you/main.php(11): double('21')
#1 {main}
thrown in /Users/Shared/you/main.php on line 5

A zsh terminal where php main.php prints 42 and then stops with Uncaught TypeError: double(): Argument #1 ($n) must be of type int, string given, shown twice.

The first call worked. The second passed a string, and strict mode refused it with a TypeError. Without the declare line, the same call returned 42: PHP turned "21" into 21. The command is the same in zsh, bash and PowerShell.

Why the Error Appears Twice​

This PHP, installed with Homebrew, both logs errors and displays them. The line starting with PHP Fatal error: is the log entry: the manual says that when error_log isn't set, errors go to "stderr in CLI". The second copy, starting with Fatal error:, is the displayed one, on standard output. Production servers usually log only.

Common Mistakes​

MistakeWhat happensDo this instead
Putting declare(strict_types=1); below other codeFatal error: strict_types declaration must be the very first statement in the scriptPut it right after <?php
Expecting it to cover other filesIt applies only to calls made from the file that declares itAdd it to every file
Passing an int to a float parameter and expecting an errorIt's allowed, the one exception to strict modeNothing to fix; int is accepted for float

Troubleshooting​

SymptomLikely causeFix
TypeError: … must be of type int, string givenA string reached a function that wants an int, in strict modeConvert the value first, such as (int) $text, after checking it
Values are converted silentlyThe file has no declare(strict_types=1);Add it at the top

FAQ​

Does strict mode check return types too?​

Yes. A function declared to return int that returns the string "5" gave int(5) in default mode, but threw TypeError: f(): Return value must be of type int, string returned in a strict file.

Should every file use it?​

Many projects do, because it turns silent conversions into clear errors. Add it to new files as you write them.

Conclusion​

declare(strict_types=1); at the top of a file makes PHP enforce scalar type declarations instead of converting values, so a wrong type fails loudly with a TypeError.

Additional Resources​