Limited Offer
60% OFF on all plans!

Data Types in PHP

Explore PHP's built-in data types including integers, floats, booleans, strings, arrays, null, and objects, and how PHP handles dynamic typing.

What Are Data Types?

A data type tells PHP what kind of value a variable holds — a number, text, true/false, a list, etc. PHP is dynamically typed: you never declare a type when creating a variable; PHP infers it from the assigned value. PHP supports 8 built-in types split into three categories: scalar, compound, and special.

1. Dynamic Typing in PHP

PHP infers the type from the value — the same variable can hold different types over time.

2. PHP's 8 Built-in Types

PHP groups its types into scalar, compound, and special categories.

Scalar Types

Scalar types hold a single value. They are the most common types you will work with in everyday PHP code.

1. Integer (int)

Whole numbers — positive, negative, or zero. PHP also supports hex, octal, binary, and underscore separators.

2. Float (float / double)

Numbers with a decimal point or written in scientific notation.

3. Boolean (bool)

Holds either `true` or `false` — the foundation of all conditional logic.

4. String (string)

A sequence of characters. PHP strings can hold text of any length.

Compound Types

Compound types can hold collections of values. Arrays are ordered maps of key-value pairs. Objects are instances of classes that group data and behaviour.

1. Array

PHP arrays are ordered maps — they can be indexed (numeric keys) or associative (string keys).

2. Object

An instance of a class that bundles related data and methods.

Special Types

PHP has two special types: `null` (a variable with no value) and `resource` (a reference to an external resource managed by PHP, such as a file handle or database connection).

1. NULL

`null` is a type with a single possible value — `null` — meaning "no value".

2. Resource

A resource is a special handle to an external resource — returned by functions like `fopen()` or database connectors.

Type Checking

PHP provides a family of `is_*()` functions and `gettype()` for type checking, plus `instanceof` for objects. PHP 8.0 also introduced the `get_debug_type()` function which returns more accurate type names.

1. gettype() and is_*() Functions

Use `gettype()` for the type name or the faster `is_int()`, `is_string()` etc. for boolean checks.

2. get_debug_type() — PHP 8.0+

Returns a more precise type name than `gettype()` — especially for objects and enums.

3. instanceof — Checking Object Type

`instanceof` checks if an object is an instance of a specific class or interface.

Type Casting

PHP performs a lot of automatic type coercion (called "type juggling"), but you can also cast explicitly using `(type)` syntax or functions like `intval()`, `floatval()`, and `strval()`.

1. Explicit Casting with (type)

Cast a value to a specific type by prepending `(int)`, `(float)`, `(string)`, `(bool)`, or `(array)`.

2. intval(), floatval(), strval()

Function-based casting — more readable and supports a base parameter for `intval()`.

3. Type Juggling (Automatic Coercion)

PHP automatically converts types in expressions — understanding this prevents hard-to-find bugs.

Type Declarations (PHP 7.0+)

Since PHP 7.0 you can declare types on function parameters and return values. PHP 7.4 added typed properties. PHP 8.0 added union types. PHP 8.1 added intersection types and the `never` return type. These features turn PHP from a loosely-typed language into an optionally strongly-typed one.

1. Parameter & Return Types — PHP 7.0+

Declare the expected type of each parameter and the return type after the colon.

2. Nullable Types — PHP 7.1+

Prefix a type with `?` to allow `null` as well as the declared type.

3. Union Types — PHP 8.0+

Declare that a parameter or return value can be one of several types using `|`.

4. Intersection Types — PHP 8.1+

Require a value to satisfy multiple type constraints simultaneously using `&`.

5. Typed Properties — PHP 7.4+

Class properties can have a declared type, enforced at assignment time.