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.

<?php 
  
    if(TRUE){  
        echo "This condition is TRUE.";
    }
  
    if(FALSE){  
        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.

Example:

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

   //Output:
   //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.

<?php
 
$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:

Example:

<?php

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

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


//Output:

//Welcome to Elevenstech
//Web Tutorials

?>

 

PHP Array

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

Example:

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

var_dump($list);

//Output:
//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.

Example

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

//Output:
//Car Model: BMW
?>  

 

PHP NULL Value

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.

Example

<?php
 
$num = NULL;
echo $num;    // this will return no output
 
// return data type
var_dump($num);
 

//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.