In this article, you will learn how to use the PHP data types. Also, I will explain how to cast these types into other data types. And how to use PHP code to get the type of the PHP variable values.

Before getting started, we just need to focus on the meaning of the PHP data types.

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.

In the following sections, we will dive more into each one to expose it with an example. But before we do that, we have to group these types into categories to be easily understandable. these categories would be like in the below list.

  1. Scalar Data Types
  2. Compound Data Types
  3. Special Data Types
PHP Data Types

In the next sections, we will discuss each one in-depth.

The PHP Scalar Data Types

We can define the scalar types as the limited group of possible values that can be assigned to the PHP variable. The scalar types such as integers, strings, floats, and booleans.

let’s get started right away with the PHP string types.

PHP String

PHP string is a list of characters that are located between single or double quotes. The max of characters should not be over 256 characters. PHP does not support the native Unicode. The string can be double-quotes or single-quotes.

PHP String Types

Anyway, the PHP supports four data types for PHP string. Which are single-quotes, double-quotes, heredoc, and nowdoc. You have to navigate to the PHP string tutorial to learn more about them.

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

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

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


The output:

Double Quotes !
Single Quotes !
This is heredoc string
This is nowdoc string

In the next section, I am going to explain the following data type which is an integer. Let’s dive right in.


PHP Int is a numerical value that can be assigned to the PHP variable. This means a number from the set -3, -2, -1, 0, 1, 2, 3.

PHP Integer Types

Anyway, the PHP supports four data types for PHP int. Which are decimal, hexadecimal, octal, and binary. You have to navigate to the PHP Int tutorial to learn more about them.

In the next PHP code, you will see how to use the four types of PHP integers.


   // Decimal
   $decimal = 500;
   echo $decimal;
   echo "<br/>";
   // Hexadecimal
   $hexadecimal = 0x5CB;
   echo $hexadecimal;
   echo "<br/>";
   // Octal
   $octal = 0264;
   echo $octal;
   echo "<br/>";

   // Binary
   $binary = 0b010101;
   echo $binary;
   echo "<br/>";


The output would be like the below.


PHP Float

PHP float is the point in the numbers or numbers that are containing a decimal point. For another definition, we can name it “double” or “real numbers”.

For more detail navigate to the PHP float tutorial. In the following code, I will show you an example of a PHP float.

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

PHP Boolean

PHP Bool or PHP Boolean refers to the value that is already assigned to the variable. It can be true or false value. And we are using it to detect the coming data, check calculation values or detect a variable value, The important thing is, it would give us a true or false value.

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


Anyway, you have to navigate to the PHP boolean tutorial to learn more about it.

In the next section, we will move to the compound data types.

Compound PHP Data Types

We can define compound data types as the data that can contain over one element each one has a different primitive PHP data type. Such as arrays, objects, callables, and iterable. Let’s see each one in a PHP code example.

PHP Array

PHP array is a list or map that contains multiple values grouped by array key. It may be includes an integer, string, boolean, or whatever data type, the key can be string or numeral index.

PHP supports two shapes for the array such as [ … ] or array( … ). In the following example, you will see how to write it in coding.

   $array_1 = array( "string", 1, ["data"], true );   

The output

    [0] => string
    [1] => 1
    [2] => Array
            [0] => data

    [3] => 1

Anyway, you have to navigate to the PHP Array tutorial to learn more about it.

PHP Object

The PHP object is an instance from the PHP class or the main data structure that is already been created by the PHP class. It can be assigned to a variable or called with $this that already refers to an individual object.

The following example shows you how to take an instance from a class as an object.

   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 examples, read the PHP object tutorial.

PHP Callable

The PHP callable or callback refers to the function that accepts another function callback in the parameter. Which is executing the code when calling the main function.

For example

   function func($param1, $param2) {
      // Your code here
   call_user_func( "func", 55, 120 );

Navigate to the PHP callable tutorial to learn more.

PHP Iterable

The first appearance of PHP iterable was in PHP version ( 7.1 ) – It is the word that can be defined in the for loop or for each. it accepts arrays or objects and it can be used as a PHP data type – Or in another definition, The iterable is everything that can be used as a loop. The followings are examples of PHP iterable.

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

You can see more example in the PHP iterable tutorial.

Special PHP Data Types

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

PHP Null

The meaning of the PHP null is no value that has been assigned to the PHP variable or a direct no value. and to check this value you have to use the built-in PHP function is_null. Let’s see the syntax of the PHP null.

The following is an example.

   echo null // no value 
To learn more about it you have to navigate to the PHP null tutorial.

PHP Resource Data Type

The PHP resource is referring to all external accesses which are all external resources that have information or data needed to be manipulated through the main source code, such as database connections, files, documents, streams, or sockets.

The following is an example for PHP resource.

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

Get PHP Data Types

In this section, you will learn how to use the gettype PHP function to expose the previous data types. In the following examples, we need to get the current data type for the following PHP variables.

To do that, you have to use the gettype() function.

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

   echo gettype($string);
   echo "<br />";

   echo gettype($integer);
   echo "<br />";

   echo gettype($bool);
   echo "<br />";

   echo gettype($array);
   echo "<br />";

The output will be as the following block.