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

Scalar Type Declarations (PHP 7.0)

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

Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan, psalm, and rector — all specialist static analysis tools. Missing type declarations or coercion bugs won't be caught by a compiler or a default linter; they require running PHPStan/Psalm configured at an appropriate level. Slightly elevated from d5 because the coercion issue is also silent at runtime (no error, just wrong behaviour), but the tooling is specialist-tier so d5 fits best.

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: add scalar type hints to all function parameters and return types, add declare(strict_types=1) at the top of each file, then run PHPStan. This is a file-by-file sweep across the codebase — not a single-line patch and not merely one component, but also not a full architectural rework. e5 is the right anchor.

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

Closest to 'persistent productivity tax' (b5). The applies_to covers all PHP contexts (web, cli, queue-worker), meaning every function and method signature is touched by this choice. Missing or incorrect type declarations impose an ongoing analysis gap — every future developer must remember to add strict_types per-file and annotate types properly. It doesn't redefine the system's shape (b9) but it is a pervasive, recurring cost across many work streams (b5).

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

Closest to 'serious trap — contradicts how a similar concept works elsewhere' (t7). The misconception field is explicit: developers expect scalar type declarations to enforce types the way they do in most statically-typed languages, but without declare(strict_types=1) PHP silently coerces values. This directly contradicts the experience of Java, C#, TypeScript, and even PHP's own stricter mode — a competent developer from any of those languages will confidently assume type enforcement and be wrong. The common_mistakes list confirms this is the dominant failure mode.

About DEBT scoring →

TL;DR

PHP 7.0 added int, float, string, bool type declarations for function parameters — the biggest type safety leap in PHP history, enabling static analysis.

Explanation

PHP 7.0 (December 2015) added scalar type hints: int, float, string, bool. With declare(strict_types=1): strict enforcement — wrong type throws TypeError. Without it: PHP coerces where possible ('5' → 5). Return types also added: function add(int $a, int $b): int. This enabled PHPStan, Psalm, and IDE analysis to verify type contracts. Previously only class/interface names and array/callable were valid hints. PHP 7.1 added nullable types (?string), PHP 7.1 added void return. PHP 8.0 added union types (int|string), PHP 8.1 added never.

Common Misconception

Scalar types in PHP always throw on mismatch — without declare(strict_types=1), PHP coerces types. Enable strict_types for actual enforcement.

Why It Matters

Scalar type declarations transformed PHP from a dynamic scripting language into one with static analysis support — enabling a whole ecosystem of type-checking tools.

Common Mistakes

  • Not using declare(strict_types=1) — coercion hides type bugs.
  • Forgetting to add return types — functions without return types can't be fully analysed.
  • Using mixed instead of proper union types — loses all type safety.

Code Examples

✗ Vulnerable
// PHP 5 — no type hints:
function add($a, $b) {
    return $a + $b; // What if strings are passed?
}
✓ Fixed
<?php declare(strict_types=1);

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

function greet(?string $name): string {
    return 'Hello, ' . ($name ?? 'World');
}

Added 23 Mar 2026
Views 60
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 1 ping T 1 ping W 2 pings T 1 ping F 2 pings S 5 pings S 2 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Scrapy 14 Amazonbot 10 Perplexity 7 SEMrush 5 Unknown AI 3 Ahrefs 3 ChatGPT 2 PetalBot 2 Google 1 Claude 1 Sogou 1 Qwen 1
crawler 49 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Add scalar type hints to all function parameters and return types. Add declare(strict_types=1) at top of each file. Run PHPStan to find missing or wrong types.
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
function [a-z]+\([^)]*\)\s*{
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✓ Auto-fixable Fix: Medium Context: Function Tests: Update


✓ schema.org compliant