Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 147 · Data Types

Data Types

Get to know every PHP data type - bool, int, float, string, array, object, callable, null and resources - and check types with gettype, get_debug_type, is_* and var_dump.

Every value in PHP has a type. The type decides what the value is and what you can do with it: you can multiply two integers, uppercase a string, loop over an array, or call a method on an object. Mix them up and you get surprising results or errors, so knowing the types is the foundation for everything that follows.

PHP has a small, well-defined set of types, grouped into three families: scalar types that hold a single value, compound types that hold many values, and two special types. This lesson introduces each one, shows you how to ask PHP what type a value has, and teaches you to read var_dump() output fluently. The following lessons in this chapter then dive into each type in depth.

The types at a glance

Family Type Example values What it's for
Scalar bool true, false Yes/no answers
Scalar int 42, -7, 0x1F, 1_000_000 Whole numbers
Scalar float 3.14, -0.5, 1.2e3 Numbers with a fractional part
Scalar string 'hello', "Total: $sum" Text and raw bytes
Compound array [1, 2, 3], ['id' => 7] Ordered lists and key/value maps
Compound object new DateTimeImmutable() Instances of classes
Compound callable 'strlen', fn($x) => $x * 2 Anything that can be called like a function
Compound iterable arrays, generators Anything you can loop over with foreach
Special null null "No value"
Special resource an open file handle A handle to something outside PHP

callable and iterable aren't separate kinds of values. They describe values of other types (strings, arrays, closures, generators) that can be used in a certain way, and you'll mostly see them in Type Declarations.

Asking for a type

PHP gives you two functions that return a value's type as a string:

  • get_debug_type() (PHP 8.0) returns modern, precise names: int, float, bool, string, array, null, and for objects the class name. Use this one.
  • gettype() is the older function. It returns historical names: integer, double (for floats), boolean, and just object for any object.
PHP gettype versus get_debug_type

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The output shows both names side by side:

TEXT
int                  integer
float                double
bool                 boolean
string               string
array                array
null                 NULL
DateTimeImmutable    object
Closure              object
resource (stream)    resource

get_debug_type() names the class (DateTimeImmutable, Closure) and the kind of resource. gettype() calls a float a double, a name left over from C. You'll still find gettype() in older code, but for error messages and debugging, get_debug_type() is clearer.

Scalar types

A scalar holds exactly one value.

bool

A boolean is true or false. Comparisons produce booleans, and if statements consume them. Every other type can be converted to a boolean: 0, 0.0, '', '0', [] and null count as false, almost everything else as true. Booleans and null has the full table.

int

An integer is a whole number. On 64-bit systems (practically all servers today) integers range from PHP_INT_MIN to PHP_INT_MAX, about ±9.2 quintillion. You can write integers in decimal, hexadecimal (0xFF), octal (0o17, PHP 8.1) or binary (0b1010), with underscores for readability (1_000_000, PHP 7.4).

float

A float (floating-point number) has a fractional part or an exponent: 2.5, 1.5e3 (1500.0), 7E-10. Floats are approximations stored in binary, so some decimal values can't be represented exactly. The famous example: 0.1 + 0.2 is not exactly 0.3. Integers and Floats explains why and how to deal with it.

string

A string is a sequence of bytes, usually text. Strings can be written with single quotes, double quotes (with variable interpolation), heredoc or nowdoc. PHP strings have no fixed character encoding; most modern code stores UTF-8. Details are in Strings and Unicode and Multibyte Strings.

PHP The four scalar types

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The output is:

TEXT
bool(true)
int(1250000)
float(4.7)
string(11) "Night Train"
int(255)
int(10)
float(1500)
float(0.30000000000000004)

Notice that literals in hex and binary are just integers: var_dump() shows their decimal value. And 1.5e3 is a float even though it has no fractional part: var_dump() prints float(1500), keeping the type visible.

Compound types

A compound value groups other values together.

array

A PHP array is an ordered map: a list of key/value pairs that keeps its insertion order. The same type covers simple lists (keys 0, 1, 2...) and dictionaries with string keys, and arrays can nest. It's PHP's most-used data structure, with its own chapter starting at Arrays.

object

An object is an instance of a class: a bundle of data (properties) and behavior (methods). PHP ships many classes, such as DateTimeImmutable and ArrayObject, and you'll write your own in Object-Oriented Programming in PHP. The simplest generic object is stdClass, which you'll get from json_decode() or by casting an array with (object).

PHP Arrays and objects in var_dump

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

In the output, array(2) means two elements at that level, and each element shows its key in brackets followed by => and the value. For the object you see object(stdClass)#1 (2): the class name, the object's ID (#1, the first object created in this script), and the number of properties. String keys appear in double quotes (["id"]), integer keys without them ([0]).

callable and iterable

These describe how a value can be used:

  • A callable is anything you can call: a function name as a string ('strtoupper'), a closure (fn ($x) => $x * 2), a first-class callable (strlen(...), PHP 8.1), or an [object, 'method'] array. is_callable() checks it. See Anonymous Functions and Closures.
  • An iterable is anything foreach can loop over: arrays and Traversable objects such as generators. See Generators.

Special types

null

null is the only value of the type null, and it means "no value". A variable is null when you assign null to it, when it was unset(), or (with a warning) when you read it before assigning. Functions often return null to say "nothing found". The type name is case-insensitive (NULL, null), but modern code writes it in lowercase.

resource

A resource is a handle to something outside PHP, such as an open file, a network connection or a process. You get one from functions like fopen(), and you release it with the matching close function (fclose()). Many old resource types have been replaced by real objects in PHP 8 (for example, curl_init() now returns a CurlHandle object), but streams from fopen() are still resources.

PHP null and resources

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

var_dump() shows a resource as resource(5) of type (stream): the number is an internal ID and may differ. After fclose(), the type becomes resource (closed). The array_search() line is a reminder that "nothing found" isn't always null: older functions often return false, which you'll learn to check carefully in Array Functions.

Checking types with is_* functions

To test for one particular type, PHP has a family of is_*() functions that return true or false:

Function True for
is_int() Integers
is_float() Floats
is_string() Strings
is_bool() Booleans
is_array() Arrays
is_object() Objects
is_null() null
is_callable() Anything callable
is_iterable() Arrays and Traversable objects
is_numeric() Numbers and numeric strings like '42' or '1.5e3'
is_scalar() int, float, string or bool
PHP Type checks

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

is_numeric('7') is true while is_int('7') is false, and the order of the checks matters: the numeric-string check comes before the generic string check. That distinction becomes important when you read user input, which always arrives as strings; see Type Juggling and Casting. The match (true) construct picks the first condition that's true; it's explained in switch and match.

Reading var_dump output

You've seen var_dump() throughout this lesson. Here's a compact reference for what each format means:

Output Meaning
bool(true) A boolean
int(42) An integer
float(0.5) / float(1500) A float; whole floats still say float
string(5) "hello" A string of 5 bytes (not characters)
NULL The null value
array(3) { ... } An array with 3 elements at this level
["key"]=> / [0]=> A string key / an integer key
object(User)#3 (2) { ... } An object of class User, object ID 3, with 2 properties
["email":protected]=> A protected property (private ones show the class name)
uninitialized(string) A typed property that has no value yet
resource(5) of type (stream) A resource with internal ID 5
enum(Status::Active) An enum case (PHP 8.1)

Tip: string(5) counts bytes. var_dump('café') prints string(5) "café" because é takes two bytes in UTF-8. When the number looks too high for the visible text, you're looking at multibyte characters.

A note on type declarations

So far you've asked values about their type after the fact. Modern PHP lets you declare the types a function accepts and returns, and PHP checks them for you:

PHP
<?php

declare(strict_types=1);

function average(int|float ...$numbers): float
{
    return array_sum($numbers) / count($numbers);
}

echo average(3, 4.5, 6);   // 4.5
// average('3') would throw a TypeError in strict mode

Declared types make code self-documenting and catch bugs early. They use the same type names you learned here, plus a few extras such as mixed, void, never and union types like int|float. Type Declarations and The PHP Type System in Depth cover the full system.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does echo gettype(2.5); print?

    gettype() uses the historical name double for floats. get_debug_type(2.5) returns float.
  2. 2. What does var_dump('naïve'); print?

    var_dump() counts bytes. The ï takes two bytes in UTF-8, so five characters are six bytes.
  3. 3. Which of these is a compound type?

    Arrays (and objects) hold other values, which makes them compound. int and bool are scalars; null is a special type.
  4. 4. What does get_debug_type(new DateTimeImmutable()) return?

    For objects, get_debug_type() returns the class name. gettype() would return just "object".
  5. 5. Which call returns true?

    '42' is a string, so is_int() is false, but is_numeric() accepts numeric strings as well as numbers.

Summary

  • Scalar types hold one value: bool, int, float, string.
  • Compound types hold many: array and object; callable and iterable describe how values can be used.
  • Special types: null ("no value") and resource (a handle to something outside PHP, such as an open file).
  • get_debug_type() (PHP 8.0) gives precise type names, including class names; the older gettype() says integer, double and object.
  • is_int(), is_string(), is_numeric() and the other is_*() functions test for one type; is_numeric() also accepts numeric strings.
  • var_dump() shows type, size and value; string(n) counts bytes, and objects show their class, ID and property count.

Next: Strings, where you'll master quotes, escapes, interpolation, heredoc and nowdoc.