100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
PHP

Data Types in PHP

A tour of PHP's scalar, compound, and special types, plus how PHP's dynamic typing and type juggling behave in practice.

PHP FoundationsBeginner8 min readJul 9, 2026
Analogies

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
<?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

Was this page helpful?

Topics covered

#PHP#PHPProgrammingStudyNotes#Programming#DataTypesInPHP#Data#Types#Scalar#Compound#StudyNotes#SkillVeris