php8.5
Home/ Manual/ language/ Types

Types

Every single expression in PHP has one of the following built-in types depending on its value: nullboolintfloat (floating-point number)stringarrayobjectcallableresource

Introduction

Every single expression in PHP has one of the following built-in types depending on its value:

  • null
  • bool
  • int
  • float (floating-point number)
  • string
  • array
  • object
  • callable
  • resource

PHP is a dynamically typed language, which means that by default there is no need to specify the type of a variable, as this will be determined at runtime. However, it is possible to statically type some aspect of the language via the use of type declarations. Different types that are supported by PHP's type system can be found at the type system page.

Types restrict the kind of operations that can be performed on them. However, if an expression/variable is used in an operation which its type does not support, PHP will attempt to type juggle the value into a type that supports the operation. This process depends on the context in which the value is used. For more information, see the section on Type Juggling.

Tip

The type comparison tables may also be useful, as various examples of comparison between values of different types are present.

Note

It is possible to force an expression to be evaluated to a certain type by using a type cast. A variable can also be type cast in-place by using the settype() function on it.

To check the value and type of an expression, use the var_dump() function. To retrieve the type of an expression, use the get_debug_type() function. However, to check if an expression is of a certain type use the is_type functions instead.

Different Types

php
<?php
$a_bool = true;   // a bool
$a_str  = "foo";  // a string
$a_str2 = 'foo';  // a string
$an_int = 12;     // an int

echo get_debug_type($a_bool), "\n";
echo get_debug_type($a_str), "\n";

// If this is an integer, increment it by four
if (is_int($an_int)) {
    $an_int += 4;
}
var_dump($an_int);

// If $a_bool is a string, print it out
if (is_string($a_bool)) {
    echo "String: $a_bool";
}
?>

8

output
bool
string
int(16)
Note

Prior to PHP 8.0.0, where the get_debug_type() is not available, the gettype() function can be used instead. However, it doesn't use the canonical type names.

Type-system Null Boolean Integer Float String Numeric-strings Array Object Enumerations Resource Callable Mixed Void Never Relative-class-types Singleton Iterable Declarations Type-juggling

In this section

Arrays

For a list of all array functions, see the array functions chapter in the documentation.

Booleans

The bool type only has two values, and is used to express a truth value. It can be either...

Callables

A callable is a reference to a function or method that is passed to another function as an...

Enumerations

Enumerations are a restricting layer on top of classes and class constants, intended to pr...

Floating-point numbers

Floating-point numbers (also known as "floats", "doubles", or "real numbers") can be speci...

Integers

An int is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}.

Iterables

Iterable is a built-in compile-time type alias for array|Traversable. From its introductio...

Mixed

The mixed type accepts every value. It is equivalent to the union type object|resource|arr...

NULL

The null type is PHP's unit type, i.e. it has only one value: null.

Never

never is a return-only type indicating the function does not terminate. This means that it...

Numeric strings

A PHP string is considered numeric if it can be interpreted as an int or a float.

Objects

To create a new object, use the new statement to instantiate a class:

Relative class types

These type declarations can only be used within classes.

Resources

A resource is a special variable, holding a reference to an external resource. Resources a...

Singleton types

Singleton types are those which allow only one value. PHP has support for two singleton ty...

Strings

A string is a series of characters, where a character is the same as a byte. This means th...

Type Juggling

PHP does not require explicit type definition in variable declaration. In this case, the t...

Type System

PHP uses a nominal type system with a strong behavioral subtyping relation. The subtyping...

Type declarations

Type declarations can be added to function arguments, return values, as of PHP 7.4.0, clas...

Void

void is a return-only type declaration indicating the function does not return a value, bu...

Source: language/types.xml · from the official PHP manual (php/doc-en)