Data Types in PHP
PHP defines eight primitive types split into three groups: scalar types (bool, int, float, string), compound types (array, object, callable, iterable), and two special types (resource, null). Because PHP is dynamically typed, a variable's type is inferred from its current value and can change as the script runs — $x = 5; $x = "five"; is perfectly legal, though modern PHP encourages constraining this with type declarations on function signatures and properties.
Cricket analogy: PHP's eight types are like a cricket squad split into batters, bowlers, and specialists (wicketkeeper, all-rounder); a player like Ben Stokes can be picked as either bat or bowl on a given day, just as $x can hold 5 then 'five' since PHP is dynamically typed.
Scalar Types
int holds platform-dependent-width whole numbers (typically 64-bit on modern systems); float (aliased double) holds IEEE 754 double-precision numbers and is subject to the same rounding caveats as any floating-point type in any language; string is a byte sequence (not natively Unicode-aware — multibyte functions like mb_strlen are needed for UTF-8 text); bool is true or false. PHP performs 'type juggling' in loose comparisons and many operators, converting types on the fly, which is powerful but a frequent source of bugs if not well understood.
Cricket analogy: A 64-bit int is like a scoreboard that can count runs into the billions without overflowing, while float behaves like a net run-rate calculation — precise but prone to rounding quirks, e.g. 0.1 + 0.2 not landing exactly on 0.3.
Compound and Special Types
array is PHP's ordered map — it behaves as both a list and a dictionary depending on the keys used. object represents any instance of a class. callable is any value usable as a function (a string function name, a [$object, 'method'] array, a Closure, or a class implementing __invoke). iterable is a union type meaning 'array or Traversable', useful for functions that accept either. null represents 'no value' and is the sole value of the null type; resource represents an external handle (like an open file or database connection) and is increasingly being replaced by dedicated objects in modern extensions.
Cricket analogy: array in PHP is like a scorecard that's both a batting order list (indexed) and a player-to-runs dictionary (associative); object is like an actual player instance such as Virat Kohli, and null represents an as-yet-unselected twelfth man slot.
<?php
declare(strict_types=1);
function describe(int|float|string|bool|null $value): string {
return match (true) {
is_int($value) => "integer: $value",
is_float($value) => "float: $value",
is_string($value) => "string: '$value'",
is_bool($value) => "bool: " . var_export($value, true),
is_null($value) => "null",
};
}
foreach ([42, 3.14, "hello", true, null] as $v) {
echo describe($v) . PHP_EOL;
}
var_dump(gettype(42)); // string(7) "integer"
var_dump(0 == "abc"); // false in PHP 8 (was true pre-8.0!)
PHP 8.0 changed a major type-juggling rule: comparing a number to a non-numeric string with == now converts the number to a string instead of the string to a number, so 0 == "abc" is false in PHP 8+ (it was true in PHP 7). This closed a long-standing class of security and logic bugs.
Never rely on loose comparison (==) for security-sensitive checks like password hash comparisons or type-sensitive array lookups — use strict comparison (===) or dedicated functions like hash_equals() to avoid type-juggling surprises.
- PHP has 8 types: bool, int, float, string, array, object, callable/iterable, and null/resource.
- PHP is dynamically typed by default; a variable's type follows its current value.
- Type juggling automatically converts types in loose comparisons and many operators.
- PHP 8 changed number-vs-string comparison semantics to reduce classic type-juggling bugs.
- Use gettype(), is_int(), is_string(), etc. or match(true) patterns to branch on runtime type.
- Prefer strict comparison (===) and strict_types for predictable, bug-resistant type behavior.
Practice what you learned
1. Which of these is NOT one of PHP's primitive types?
2. In PHP 8+, what does 0 == "abc" evaluate to?
3. Why is PHP's built-in `string` type not automatically Unicode-safe?
4. What kind of type is `iterable` in PHP?
5. Which comparison operator should be preferred for security-sensitive equality checks?
Was this page helpful?
You May Also Like
PHP Syntax and Variables
Covers PHP's basic script structure, statement termination, comments, and how variables are declared, scoped, and named in PHP 8.x.
Operators and Expressions
Explains PHP's arithmetic, comparison, logical, assignment, and null-coalescing operators, along with operator precedence and expression evaluation order.
Type Declarations and Strict Types
PHP supports optional scalar and object type declarations on parameters, return values, and properties, with strict_types controlling how aggressively the engine coerces mismatched values.
Enums in PHP
PHP 8.1 enums provide a first-class way to define a fixed set of named values, with optional scalar backing, methods, interfaces, and constants attached.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics