Why this matters
With strict_types enabled, function calls with incorrect types throw TypeError instead of silently converting values. This helps catch type issues early in development, making code more robust and preventing unexpected behavior.
At the top of your PHP files (before any code), declare `declare(strict_types=1)`. This forces PHP to enforce the declared parameter and return types strictly, avoiding automatic type coercion.
With strict_types enabled, function calls with incorrect types throw TypeError instead of silently converting values. This helps catch type issues early in development, making code more robust and preventing unexpected behavior.
Side-by-side examples engineers can pattern-match during review.
<?php
// file without strict_types
function sum(int \$a, int \$b): int {
return \$a + \$b;
}
echo sum("5", "7"); // coerces strings to ints
?><?php
declare(strict_types=1);
function sum(int \$a, int \$b): int {
return \$a + \$b;
}
echo sum("5", "7"); // throws TypeError
?><?php
// file without strict_types
function sum(int \$a, int \$b): int {
return \$a + \$b;
}
echo sum("5", "7");
?><?php
declare(strict_types=1);
function sum(int \$a, int \$b): int {
return \$a + \$b;
}
echo sum("5", "7");
?>From the same buckets as this rule.