Data Types Available in PHP

Quick Summary
- PHP has eight basic data types: scalar integer, float, boolean and string, compound array and object, and special null and resource.
- Use var_dump() or gettype() to check a value's type; a variable is null if assigned null, never set or removed with unset().
- Since PHP 8.0, several extensions such as cURL return objects instead of resources, which reference things outside PHP like open files.
PHP has eight basic data types: four scalar types that hold a single value (integer, float, boolean and string), two compound types that can hold several values (array and object), and two special types (null and resource). To check the type of a value, use var_dump() or gettype().
- Integer
- Float/Double
- Boolean
- String
- Array
- Object
- Null
- Resource
Integers: Integers are whole numbers, without a decimal point (…, -2, -1, 0, 1, 2, …)
Floating Point Numbers or Doubles: Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) are decimal or fractional numbers. Example: $a = 1.234; $b = 10.2e3;
Strings: Strings are sequences of characters. The simplest way to specify a string is to enclose it in single quotes (e.g. ‘Hello world!’), however, you can also use double quotes (“Hello world!”). Double quotes also replace variables with their values; see single vs double quotes in PHP.
Booleans: Booleans are like a switch: they have only two possible values, true or false. Example: $show_val = true;
Arrays: An array is a variable that can hold more than one value at a time. The PHP array quick reference shows how to create, sort and search them.
Objects: An object is a data type that not only allows storing data but also information on, how to process that data. An object is a specific instance of a class that serves as templates for objects. Objects are created based on this template via the new keyword.
Null: The special value null means a variable has no value. A variable is null if it has been assigned null, hasn’t been set yet or has been removed with unset().
Resources: A resource is a special variable that holds a reference to something outside PHP, such as an open file from fopen(). Since PHP 8.0, several extensions, cURL among them, return objects instead of resources.
Related articles
- declaring parameter and return types: make a function accept and return only the types you expect

