W3docs

Understanding PHP Data Types

Learn PHP's eight data types — int, float, bool, string, array, object, null, and resource — with runnable examples, type checking, and casting rules.

A data type describes what kind of value a variable holds and what operations are valid on it. PHP is dynamically typed: you never declare a type, you just assign a value and PHP infers the type from it. The same variable can even change type during a script. Understanding the eight built-in types — and how PHP converts between them — is the foundation for writing predictable PHP code.

This chapter covers all of PHP's types, how to inspect a value's type at runtime, and the type-juggling rules that trip up most beginners.

The Eight PHP Data Types

PHP has eight primitive types, grouped into three families:

FamilyTypes
Scalar (single value)int, float, bool, string
Compound (collections)array, object
Specialnull, resource

You can ask PHP what type any value is with the gettype() function, or check a specific type with functions like is_int(), is_string(), or is_null().

<?php
$value = 42;
echo gettype($value); // integer
$value = "now I'm a string";
echo PHP_EOL . gettype($value); // string

Scalar Types

Scalar types hold a single value.

Integer

An integer is a whole number, positive or negative, with no decimal point. Learn more in PHP Numbers.

<?php
$num = 42;        // decimal
$negative = -7;
$hex = 0x1A;      // hexadecimal (26)
$binary = 0b101;  // binary (5)
echo "$num $negative $hex $binary"; // 42 -7 26 5

Float

A float (floating-point number) holds a number with a decimal point or an exponent. Note that floats are approximate, so never compare them for exact equality.

<?php
$price = 12.99;
$scientific = 1.2e3; // 1200
echo $price + $scientific; // 1212.99

Boolean

A boolean holds only true or false. It is the type produced by comparisons and used by conditions like if.

<?php
$isActive = true;
$hasError = false;
var_dump($isActive); // bool(true)

String

A string is a sequence of characters. Double-quoted strings interpolate variables; single-quoted strings do not. See PHP Strings for the full story.

<?php
$name = "John";
echo "Hello, $name!";  // Hello, John!
echo PHP_EOL . 'Hello, $name!'; // Hello, $name!  (no interpolation)

Compound Types

Compound types group multiple values together.

Array

An array stores an ordered map of key/value pairs in a single variable. Use the short [] syntax in modern PHP.

<?php
$fruits = ["apple", "banana", "cherry"];
$prices = ["apple" => 1.20, "banana" => 0.50]; // associative
echo $fruits[1];          // banana
echo PHP_EOL . $prices["apple"]; // 1.2

Object

An object is an instance of a class — it bundles data (properties) with behavior (methods). Create one with the new keyword.

<?php
class Person {
    public function __construct(public string $name) {}
    public function greet(): string {
        return "Hi, I'm {$this->name}";
    }
}

$person = new Person("Ada");
echo $person->greet(); // Hi, I'm Ada

Special Types

NULL

null represents a variable with no value. A variable is null if it has been assigned the constant null, has not been set yet, or has been unset(). Check for it with is_null() or === null.

<?php
$age = null;
var_dump(is_null($age)); // bool(true)

Resource

A resource is a special variable holding a reference to an external object such as an open file or a database connection. You don't create resources directly — functions like fopen() return them. Resources are released automatically when no longer used.

Type Juggling and Casting

Because PHP is dynamically typed, it automatically converts ("juggles") types when an operation needs it. For example, a numeric string becomes a number in arithmetic:

<?php
$result = "5" + 3; // the string "5" is converted to int 5
echo $result; // 8

A string that only starts with digits (like "5 apples") raises a warning in modern PHP, so convert deliberately with a cast instead.

When you need explicit control, cast a value by writing the target type in parentheses:

<?php
$text = "12.99";
$asFloat = (float) $text; // 12.99 as a float
$asInt = (int) $text;     // 12 (truncated)
var_dump($asInt); // int(12)

Watch out for truthiness: when cast to bool, the values 0, 0.0, "", "0", [], and null are all false; almost everything else is true.

<?php
var_dump((bool) "0");   // bool(false)
var_dump((bool) "0.0"); // bool(true)  — surprising!

When to Use Which Type

  • Use int/float for quantities and math; reach for PHP Numbers helpers when formatting.
  • Use string for text and identifiers.
  • Use bool for flags and the results of comparisons.
  • Use array to hold lists or maps of related values.
  • Use object to model real-world entities with both data and behavior.
  • Use null to signal "no value yet" rather than a fake placeholder like 0 or "".

Summary

PHP infers a value's type automatically from what you assign, and supports eight types across the scalar, compound, and special families. Inspect types with gettype() and the is_* functions, and control conversions with explicit casts. Mastering type juggling — especially the bool and numeric-string rules — prevents a whole class of subtle bugs. Next, see how types interact with operators and how to store them in variables.

Practice

Practice
Which of the following are valid data types in PHP?
Which of the following are valid data types in PHP?
Was this page helpful?