← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

declare(strict_types=1)

PHP PHP 7.0+ Intermediate
debt(d3/e5/b5/t7)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list phpcs, phpstan, and php-cs-fixer — all mainstream PHP static analysis tools that can trivially flag files missing the declare(strict_types=1) header. This is a well-known sniff in phpcs and a common rule in php-cs-fixer, so the default toolchain catches it without specialist configuration.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix says to add the declaration to every PHP file — but the common_mistakes warn that doing so after a large codebase exists reveals many TypeErrors, requiring fixes across many files. It's not a single-line swap for an established project; it becomes a multi-file remediation effort addressing all the coercion sites the declaration exposes.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). The declaration applies to web, cli, and queue-worker contexts and must be consistently maintained in every file. Missing it in any file silently reverts that file to coercive mode. This imposes an ongoing per-file discipline tax across all work streams — every new file must include it — but it doesn't reshape the architectural shape of the system, keeping it below b7.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The canonical misconception is explicit: developers believe declaring strict_types=1 in one file enables strict mode application-wide, but it is strictly file-scoped. Calls from non-strict files into strict-mode functions still use coercive typing. This directly contradicts the intuition that a 'mode switch' is global, and the behavior differs from how similar language-wide pragma systems (e.g., Python's __future__ imports, Perl's use strict) work — earning t7.

About DEBT scoring →

Also Known As

declare strict_types strict_types=1 PHP strict mode

TL;DR

Enables strict scalar type checking for function arguments and return values in a PHP file, preventing silent type coercion.

Explanation

When declare(strict_types=1) is placed at the top of a PHP file, scalar type declarations (int, float, string, bool) are enforced strictly — passing the wrong type throws a TypeError rather than silently coercing the value. Without strict mode, PHP will coerce '1abc' to 1 for an int parameter, masking bugs. Strict types apply only to calls made from the file where the declaration is present, not to the function definition file. It is a best practice for all new PHP 7+ code.

Common Misconception

declare(strict_types=1) affects the entire application once declared anywhere. It only affects the file it is declared in — calls from files without strict mode still use coercive typing even when calling functions defined in strict-mode files.

Why It Matters

declare(strict_types=1) makes PHP enforce type declarations without coercion — passing '42' to an int parameter throws a TypeError instead of silently converting, catching a whole class of bugs at development time.

Common Mistakes

  • Not adding strict_types=1 to every file — it only applies to the file it is declared in, not the whole project.
  • Expecting strict_types to affect calls into standard library functions — it only affects user-defined function calls.
  • Adding strict_types after having written a lot of code and being surprised by the number of TypeErrors it reveals — adds up tech debt.
  • Confusing strict_types (scalar coercion) with typed properties (class property type enforcement, always strict).

Code Examples

💡 Note
Add declare(strict_types=1) to every new PHP file. It won't fix existing callers, but it makes your code explicit and catches bugs early.
✗ Vulnerable
<?php // Without strict_types:
function double(int $n): int { return $n * 2; }
double('4'); // Returns 8 — silent coercion
double('4.5abc'); // Returns 8 — '4.5abc' coerced to 4

<?php declare(strict_types=1); // With strict_types:
double('4'); // TypeError: must be int, string given
✓ Fixed
<?php declare(strict_types=1);

// Without strict_types: PHP coerces '42' to 42 silently
// With strict_types: '42' passed to int param throws TypeError

function add(int $a, int $b): int {
    return $a + $b;
}

add(1, 2);     // ✓ fine
add('1', '2'); // ✗ TypeError: must be int, string given

// strict_types=1 ONLY affects calls made FROM that file
// It does not affect the callee's behaviour — it's per-file

Added 15 Mar 2026
Edited 22 Mar 2026
Views 113
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 1 ping S 1 ping M 1 ping T 2 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 0 pings S 0 pings M
No pings yet today
No pings yesterday
Scrapy 24 Perplexity 11 Amazonbot 10 ChatGPT 7 SEMrush 6 Google 5 Ahrefs 5 Unknown AI 4 PetalBot 4 Bing 3 Qwen 2 Twitter/X 2 Brave Search 2 Meta AI 1 Applebot 1
crawler 81 crawler_json 5 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Operator general The symbols that combine and compare values: arithmetic (+ - * /), comparison (=== < >), logical (&& || !), and assignment (= += .=).

One character separates assignment from comparison (= vs ==) and loose from strict equality (== vs ===) — and each of those single characters is a classic bug class. Short-circuiting, meanwhile, is the standard idiom for safe access.

💡 Default to === everywhere; when a condition is always true, look for a single = first.

Ask Codex about Operator →
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
PHP Syntax php The grammar of PHP: code inside <?php tags, statements ending in semicolons, $-prefixed variables, and C-style blocks with braces.

Nearly every PHP parse error and beginner bug traces back to one of five syntax rules: the semicolon, the $ prefix, quote interpolation, == vs ===, and the open tag. Knowing these cold makes error messages readable.

💡 When a parse error points at line N, read line N-1 first — the missing semicolon or brace is almost always above.

Ask Codex about PHP Syntax →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Add declare(strict_types=1) as the very first statement in every PHP file — it prevents silent type coercion in function calls
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
PHP file without declare(strict_types=1) at the top
Auto-detectable: ✓ Yes phpcs phpstan php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: File


✓ schema.org compliant