Why You Should Enable declare(strict_types=1) in Every PHP Project
This article explains what the PHP declare(strict_types=1) statement does, shows how it enforces strict type checking with concrete code examples, discusses common pitfalls, and offers practical advice on adopting it across new and existing codebases.
Introduction
If you are a PHP developer, you may have seen the declare(strict_types=1) statement at the top of some files without knowing its purpose. This article explains what it is and how it improves type safety in PHP code.
What is declare(strict_types=1) ?
The statement enables PHP's strict mode and forces strict type checking for function parameters and return values. It was added in PHP 7.0 and is available in PHP 8 projects.
When enabled, PHP throws a TypeError if a function receives an argument of the wrong type or returns a value that does not match its declared return type.
Example without strict types
Consider a simple function without strict typing:
function add(int $a, int $b): int {
return $a + $b;
}
echo add('1', '2'); // Output: 3PHP silently converts the string arguments to integers and returns 3. While convenient, this can hide bugs.
Enabling strict types
Adding declare(strict_types=1); at the top of the file changes the behavior:
declare(strict_types=1);
function add(int $a, int $b): int {
return $a + $b;
}
echo add('1', '2'); // Fatal error: Uncaught TypeErrorNow PHP throws a fatal error because the arguments are strings, not integers.
Return type mismatch example
If a function declares an int return type but returns a float, the mismatch is also caught when strict types are enabled:
declare(strict_types=1);
function add(float $a, float $b): int {
return $a + $b;
}
echo add(1.25, 2.25); // Fatal error: Return value must be of type int, float returnedThis highlights precision loss that would otherwise be silently cast to an integer.
Should you use declare(strict_types=1) ?
In practice, enabling strict types in every PHP file is advisable. It helps catch type‑related bugs early, especially when adding it to legacy codebases.
Even though PHP is dynamically typed, using strict types together with type hints and return type declarations raises code quality. If you cannot enable strict types for some reason, at least use type hints and return types as a minimum.
Practical ways to adopt strict types
Many developers configure their IDE templates to automatically insert the statement. For example, a PhpStorm file template might start with:
<?php
declare(strict_types=1);
#parse("PHP File Header.php")
#if (${NAMESPACE})
namespace ${NAMESPACE};
#end
class ${NAME} {
}Laravel users can also add the statement to stubs generated by Artisan commands such as php artisan make:controller, ensuring newly created files are strict‑type ready.
Steps to adopt strict types safely
Update incorrect return types to match the declared type.
Correct mismatched parameter type hints.
Adjust function bodies to return values of the proper type.
Fix calling code that passes arguments of the wrong type.
Before applying strict types to existing code, ensure you have a comprehensive test suite, as the stricter checks may cause previously unnoticed errors to surface.
Tools like PHPStan can help identify type mismatches automatically.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
