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().

  1. Integer
  2. Float/Double
  3. Boolean
  4. String
  5. Array
  6. Object
  7. Null
  8. 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.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More