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

Scalar Type Declarations (PHP 7.0)

php PHP 7.0+ Beginner

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 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 1 ping W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 3 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Amazonbot 10 Perplexity 7 Unknown AI 3 ChatGPT 2 SEMrush 2 Google 1 Ahrefs 1
crawler 25 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