Scalar Type Declarations (PHP 7.0)
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');
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
32
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 10
Perplexity 7
Unknown AI 3
ChatGPT 2
SEMrush 2
Google 1
Ahrefs 1
Also referenced
How they use it
crawler 25
pre-tracking 1
Related categories
⚡
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