PHP Data Types
Also Known As
PHP types
PHP type system
PHP scalar types
TL;DR
PHP's eight primitive types — bool, int, float, string, array, object, callable, null — and how the type system coerces between them.
Explanation
PHP is dynamically typed with eight primitive types: bool, int, float (double), string, array, object, callable, and null. PHP 8.0 formalised mixed (any type) and never (no return). Type coercion rules are nuanced: '1' == 1 is true, '0' == false is true, but null == false and null == 0 are also true. PHP 8.0 changed 0 == 'foo' to false (was true in PHP 7). Typed properties (PHP 7.4+), union types (8.0), and strict_types enforce types at runtime. Understanding coercion rules is critical for security (authentication bypass via type juggling) and correctness.
Common Misconception
✗ PHP is a loosely typed language so type declarations are optional extras. Since PHP 7, scalar type declarations and return types are enforced — and with declare(strict_types=1) PHP rejects all implicit coercions, making it behave like a strictly typed language at call boundaries.
Why It Matters
PHP has eight primitive types and loose type juggling between them — understanding how types coerce ensures predictable behaviour and prevents the class of bugs that strict_types=1 is designed to catch.
Common Mistakes
- Not enabling strict_types=1 — PHP silently coerces '42' to 42 for int parameters without it.
- Using == to compare values of different types — '0' == false == null == 0 in loose comparison.
- Forgetting that array is a first-class type in PHP, not an object — json_encode behaviour differs.
- Using float for currency — floating-point precision errors accumulate; use integer cents or bcmath.
Code Examples
✗ Vulnerable
// Loose comparison type confusion:
var_dump(0 == 'a'); // true in PHP 7, false in PHP 8 — changed!
var_dump('' == false); // true
var_dump('0' == false); // true
var_dump(0 == null); // true
// Use === always for predictable comparisons
✓ Fixed
// PHP 8 scalar types:
\$int = 42; // PHP_INT_MAX = 9223372036854775807 on 64-bit
\$float = 3.14; // IEEE 754 double — avoid for money (use int cents)
\$str = 'hello'; // byte string — mb_* functions for Unicode
\$bool = true;
// Compound:
\$arr = [1, 'two', 3.0]; // ordered map
\$assoc = ['key' => 'value'];
\$obj = new stdClass();
\$null = null;
// PHP 8 type system additions:
// union: int|string
// intersection: Iterator&Countable (8.1)
// never, void, mixed
// Enums with backed types (8.1)
// Type juggling changes in PHP 8:
var_dump(0 == 'foo'); // false in PHP 8 (was true in PHP 7)
var_dump(0 == ''); // false in PHP 8 (was true in PHP 7)
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Perplexity 6
Unknown AI 4
Ahrefs 2
ChatGPT 1
Google 1
Also referenced
How they use it
crawler 19
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Use PHP's type system fully: declare return types, use int over float for counts, use bool not int for flags, and use null explicitly with nullable types
📦 Applies To
PHP 7.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Function returning 0/1 instead of bool; null or false mixed return type; missing return type declarations
Auto-detectable:
✓ Yes
phpstan
psalm
phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Low
Context: Function