PHP data types refer to the various kinds of values that can be used and manipulated within a PHP script. Each data type in PHP specifies the type of data that a variable can hold, and it determines the operations that can be performed on that data.

Accordingly, the PHP data types are ten primitive data types used to define the PHP variable values. Such as integer, string, float, boolean, array, object, and other types such as null, and resources.

which can be broadly categorized into the following:

  • Scalar Data Types:
    • integer
    • float (Floating-point number)
    • string
    • boolean
  • Compound Data Types
    • aray
    • object
    • callable
    • iterable
  • Special Data Types
    • resource
    • null
PHP Data Types

In the next sections, we will discuss each one in-depth. Let’s start with scalar types.

Exploring PHP Scalar Types

Scalar types in PHP refer to a restricted set of values that can be assigned to a variable. These include integers, strings, floats, and booleans.

Let’s get started with PHP string types.

PHP String

A PHP string is a series of characters enclosed within single or double quotes. The string’s length should not surpass 256 characters, and PHP lacks native support for Unicode. Strings may be enclosed using either double or single quotation marks.

Here is a figure showing you the types of PHP strings.

PHP String Types

PHP supports four types of string syntax: single-quotes, double-quotes, heredoc, and nowdoc. To explore these in detail, navigate to the PHP string tutorial.

Anyway, In the following PHP code, you will see how to use the four types of PHP strings.

   // => Double Quotes
   echo "Double Quotes !"; // Output: Double Quotes !
   echo "<br/>"; 

   // => Single Quotes
   echo 'Single Quotes !'; // Output: Single Quotes !
   echo '<br/>';
   // => Heredoc
   $heredpoc = <<<IDENTIFIER
      This is heredoc string <br/>
   echo $heredpoc; // Output: This is heredoc string
   // => Nowdoc
   $nowdoc = <<<'EOUT'
      This is nowdoc string <br/>
   echo $nowdoc; // Output: This is nowdoc string

In the next section, I will explain the data type ‘integer.’ Let’s dive right in.

PHP Integer Data Type

The PHP Integer represents a numeric value that can be assigned to a PHP variable, covering numbers from the set -3, -2, -1, 0, 1, 2, 3. PHP offers four data types for handling integers: decimal, hexadecimal, octal, and binary. For additional information, you can navigate to the PHP Int tutorial.

PHP Integer Types

The example below shows you all types of PHP integers.


 // Decimal
 $decimal = 500;
 echo $decimal; // output: 500
 // Hexadecimal
 $hexadecimal = 0x5CB;
 echo $hexadecimal; // output: 1483
 // Octal
 $octal = 0264;
 echo $octal; // output: 180

 // Binary
 $binary = 0b010101;
 echo $binary; // output: 21

Let’s move on to PHP float in the section below.

PHP Float

PHP float represents numbers containing decimal points, also known as ‘double’ or ‘real numbers.’ For further information, navigate to the PHP float tutorial.

Let’s take a look at an example.


 // Float
 $float = 5.15; 
 echo $float; // the output: 5.15

Let’s move on to the last scalar type of PHP data, which is the boolean type.

PHP Boolan

PHP Bool or PHP Boolean denotes a value assigned to a variable, capable of being either true or false. Furthermore, boolean values serve to detect incoming data, verify calculation results, or assess variable values. Crucially, they yield a binary true or false outcome.

For example:

 // True Boolean Value
 $true = true; 
 echo $true; // the output: 1
 // False Boolean Value
 $false = false;  
 echo $false; // the output: 

For additional information about the PHP float data type, please navigate to this tutorial.

In the next section, we will delve into compound data types. Let’s get started.

Understanding the Compound PHP Data Types

We can define compound data types as data that can contain more than one element; each element has a different primitive PHP data type.

Examples of these compound data types include arrays, objects, callables, and iterables. Now, let’s explore each one through a PHP code example.

PHP Array

PHP array is a list or map, encompassing various values organized by an array key. These values can span integers, strings, booleans, or any other data type. The array key can be either a string or a numerical index.

PHP supports two syntaxes for arrays, either [ … ] or array( … ). In the following example, you will see how to implement it in code.

 $array_1 = array( "string", 1, ["data"], true );   
 // Output:
      [0] => string
      [1] => 1
      [2] => Array
              [0] => data
      [3] => 1

PHP Object

The PHP object is an instance derived from a PHP class or the primary data structure created by the PHP class. It can be assigned to a variable or invoked using $this, which already points to a specific object.

Let’s see an example.


class Car {

 private string $model;

   public function setModel($model) {
     $this->model = $model;


 // The object of this class 
 $object = new Car();

 // Define a new object 
 $obj = new stdClass(); 

For more details about PHP objects, please refer to this tutorial. Anyway, let’s move on to the following section.

PHP Callable

a callable refers to a type of value that can be called as a function. This can include regular functions, methods of an object, anonymous functions, and certain other language constructs. Essentially, anything that can be invoked like a function is considered callable in PHP.

For example:

function exampleFunction($value) {
    echo "Example Function: $value\n";

class ExampleClass {
    public function exampleMethod($value) {
        echo "Example Method: $value\n";

// Callable functions
$callableFunction = 'exampleFunction';
$callableMethod = [new ExampleClass(), 'exampleMethod'];

// Call the callables
call_user_func($callableFunction, "Hello");
call_user_func($callableMethod, "World");

To learn more about PHP callable, read this tutorial.

Let’s move on to PHP iterable, which is the last compound type in PHP data types.

PHP Iterable

The concept of PHP iterable first appeared in PHP version 7.1. It is a term that can be employed in the for loop or foreach. It accepts arrays or objects and can be utilized as a PHP data type.

In another definition, iterable encompasses anything suitable for use in a loop. Let’s see an example.


 function exact_loop( iterable $data ) {
	 foreach( $data as $number ) {
		 echo $number;
 exact_loop( [1,2,3,4,5,6,7,8,9] );

You can learn more about PHP iterables by navigating to this tutorial.

Let’s complete the remaining parts of this tutorial, which include the special data types.

Special PHP Data Types

There are two special data types in PHP: Null and Resource. Let’s explore each one with an example.

PHP Resource Data Type

The PHP ‘resource’ type pertains to external accesses, representing various external resources containing information or data that requires manipulation within the main source code. Examples of such resources include database connections, files, documents, streams, and sockets.

Let’s see an example.


 $file = fopen("text", "w");
 echo $file; // Resource id #5
 echo get_resource_type( $file ); // stream

For additional details about this section, read this tutorial.

Anyway, let’s proceed to the null data type in PHP.

PHP Null Data Type

The PHP null signifies the absence of a value assigned to a PHP variable, indicating a direct lack of value. To check for this condition, you can use the built-in PHP function is_null. Now, let’s delve into the syntax of the PHP null with a simple code example.

<?php echo null ?> // no value   

To delve deeper into this topic, you can navigate to the PHP null tutorial.

By the way, there’s another tool you can use to determine the data type in PHP, and that’s the built-in function gettype. Now, let’s explore how to use this function and gain insights into the data types in our PHP code.

Understanding the gettype Function in PHP

The gettype function in PHP is a built-in function that is used to determine the data type of a variable. It returns a string representing the data type of the given variable. This function is helpful when you need to dynamically check and handle different data types within your PHP code. By using gettype, you can ascertain whether a variable is an integer, string, array, object, or another data type, allowing for more robust and flexible programming.

Let’s see an example.


 $string  = "";
 $integer = 55;
 $bool    = true;
 $array   = array();

 echo gettype($string); // string

 echo gettype($integer); // integer

 echo gettype($bool); // boolean

 echo gettype($array); // array

Let’s summarize it.

Wrapping Up

PHP data types play a crucial role in defining and manipulating values within a PHP script. They categorize variables based on the type of data they can hold, influencing the operations that can be performed on them.

This article provided an overview of ten primitive PHP data types, including scalar types like integer, float, string, and boolean; compound types like array, object, callable, and iterable; and special types like null and resource.

The exploration began with an in-depth look at scalar types, delving into PHP string, integer, float, and boolean data types, along with practical examples to illustrate their usage.

The journey continued with a comprehensive examination of compound data types, showcasing PHP array, object, callable, and iterable, each demonstrated through code examples for better understanding.

Special data types, including null and resource, were then unveiled, shedding light on their significance and practical applications within PHP scripts.

The article concluded with a discussion on the gettype function, a valuable tool for dynamically checking and handling different data types in PHP code.

By understanding and mastering these PHP data types, developers can create more efficient, flexible, and reliable scripts to meet the diverse requirements of web development.