PHP Data Types


Data Types define the type of data a variable can store. PHP allows eight different types of data types. All of them are discussed below. There are pre-defined, user-defined, and special data types.

The predefined data types are:

  • Boolean
  • Integer
  • Double
  • String

The user-defined (compound) data types are:

  • Array
  • Objects

The special data types are:

  • NULL
  • resource


PHP Boolean

Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.

        echo "This condition is TRUE.";
        echo "This condition is FALSE."; 



PHP Integer

Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.

Rules for integers:

  • An integer must have at least one digit
  • An integer can be either positive or negative
  • An integer must not have a decimal point
  • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation
  • The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.


    $dec1 = 34;  
    $oct1 = 0243;  
    echo "Octal number: " .$oct1. "</br>";  
    echo "Decimal number: " .$dec1. "</br>"; 

   //Decimal number: 34
   //Octal number: 163


PHP Double

Can hold numbers containing fractional or decimal parts including positive and negative numbers or a number in exponential form. By default, the variables add a minimum number of decimal places. The Double data type is the same as a float as floating-point numbers or real numbers.

$val1 = 50.85;
$val2 = 654.26;
$sum = $val1 + $val2;
echo $sum;
//Output : 705.11


PHP String

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

A string can be any text inside quotes. You can use single or double quotes:



$a = "Welcome to Elevenstech";
$b = 'Web Tutorials';

echo $a;
echo "<br>";
echo $b;


//Welcome to Elevenstech
//Web Tutorials



PHP Array

Array is a compound data type that can store multiple values of the same data type.


$list = array( 5, 10, 15);
echo "First Value: $list[0]\n";
echo "Second Value: $list[1]\n";
echo "Third Value: $list[2]\n";


//First Value: 5
//Second Value: 10
//Third Value: 15


PHP object

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


     class car {  
          function model() {  
               $model_name = "BMW";  
               echo "Car Model: " .$model_name;  
     $obj = new car();  
     $obj -> model();  

//Car Model: BMW



These are special types of variables that can hold only one value i.e., NULL.

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


$num = NULL;
echo $num;    // this will return no output
// return data type

//Output: NULL


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.

 For example - a database call. It is an external resource.