PHP Data Types

Data Types define the type of data a variable can store.

PHP supports 8 primitive data types that can be categorized further in 3 types.

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

Scalar Types

It can hold only single value.


It holds only two values: TRUE (1) or FALSE (0).

Booleans are like a switch it has only two possible values either 1 (true) or 0 (false). It is often used with conditional statements.


    if (TRUE)  
        echo "This condition is TRUE.";  
    if (FALSE)  
        echo "This condition is FALSE.";  
PHP Boolean Example – Output


Integer means numeric data with a negative or positive sign.

Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16). It holds only whole numbers, i.e., numbers without fractional part or decimal points.


    $dec1 = 34;  
    $oct1 = 0243;  
    $hexa1 = 0x45;  
    echo "Decimal number: " .$dec1. "</br>";  
    echo "Octal number: " .$oct1. "</br>";  
    echo "HexaDecimal number: " .$hexa1. "</br>";  
PHP Integer Example – Output

The PHP var_dump() function returns the data type and value.

$x = 5985;
Output of var_dump() function


A floating-point number is a number with a decimal point.


    $n1 = 19.34;  
    $n2 = 54.472;  
    $sum = $n1 + $n2;  
    echo "Addition of floating numbers: " .$sum;  
PHP Float Example – Output


A string is a sequence of characters, like “Hello world!”.

A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.

String values must be enclosed either within single quotes or in double quotes. But both are treated differently. 


    $company = "";  
    //both single and double quote statements will treat different  
    echo "Hello $company";  
    echo "</br>";  
    echo 'Hello $company';  
PHP String Example – Output

Single quoted strings will display things almost completely “as is.” Variables and most escape sequences will not be interpreted.

Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated.

Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

PHP Array

An array stores multiple values in one single variable.

An array is a compound data type.


    $bikes = array ("Royal Enfield", "Yamaha", "KTM");  
    var_dump($bikes);   //the var_dump() function returns the datatype and values  
    echo "</br>";  
    echo "Array Element1: $bikes[0] </br>";  
    echo "Array Element2: $bikes[1] </br>";  
    echo "Array Element3: $bikes[2] </br>";  
PHP Array Example – Output

You will learn more about array in later chapters of this tutorial.

PHP object

Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.

Classes and objects are the two main aspects of object-oriented programming.


     class bike {  
          function model() {  
               $model_name = "Royal Enfield";  
               echo "Bike Model: " .$model_name;  
     $obj = new bike();  
     $obj -> model();  
PHP Object Example – Output

Special Types

There are 2 special data types in PHP.

PHP Resource

Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. 

We will discuss resources in detail in further articles.


A database call. It is an external resource.

PHP Null

Null is a special data type that has only one value: NULL

If a variable is created without a value, it is automatically assigned a value of NULL.

A variable of data type NULL is a variable that has no value assigned to it.

Variables can also be emptied by setting the value to NULL.

A variable of type NULL is a variable without any data.


    $nl = NULL;  
    echo $nl;   //it will not give any output  

You will learn a lot more about arrays, objects and resources in later chapters of this tutorial.