PHP Data Types

As PHP is an interpreted language it is not required to declare the data type. In PHP data types are used to define the type of value assigned to the variable. The memory allocated to variables is on the basis of the data types. The PHP function var_dump() is used to return the data type and value of a variable. There are eight primitive data types in PHP, which are further divided into three categories.

PHP - Datatypes

Scalar Types

Scalar types are the predefined data types that hold a single value. There are four scalar data types which include:

  1. String
  2. Integer
  3. Float (or Double)
  4. Boolean

PHP String

The string data type is the sequences of alphanumeric characters. There is basically a non-numeric data type. A string can be letters, words, numbers, and special characters. String values will be enclosed in between single quotes or (‘ ’)  double quotes (“ ”). While assigning a string to a variable they are considered the same but it is different in some cases. 

We can look into the difference with an example:


<?php
 $welcome = “Welcome to PHP tutorial”;
 $intro = ‘Where we learn to code in PHP’;
 echo “$welcome !. $intro”;
?>

Output:


Welcome to PHP tutorial !. Where we learn to code in PHP

<?php
 $welcome = “Welcome to PHP tutorial”;
 $intro = ‘Where we learn to code in PHP’;
 echo ‘$welcome !. $intro’;
?>

Output:


$welcome !. $intro

In the above examples, we can see that we have assigned the sting into variables in both single quote and double quote, and both are treated equally. And in the first example while printing the variables using double it printed the value of the variable but in the case of the second example, we printed the variables using a single quote it printed what is inside the single quote instead of the value of the variable. It is the only difference between the single quote and the double quote.

PHP Integer

The integer data type is the numeric data type; it is the whole number. It can be positive or negative. As it is a whole number it can’t be a fractional part or decimal point number. An integer may be decimal (with base 10), octal (with base 8) or hexadecimal (with base 16) decimal is taken as the default. There is no declaration needed for integers in PHP.
 


<?php
 $age = 23;
 $sum = 10 + 5;
 echo $age;
 echo “\n”;
 echo $sum;
?>


Output:


23
15

To differentiate decimal, octal, and hexadecimal in PHP we use 0 at first for octal and 0x at first for hexadecimal.

For Example ,


<?php
    $decimal = 10;
    $octal = 015;
    $hexadecimal = 0x22;
    echo "Decimal: $decimal \n";
    echo "Octal: $octal \n";
    echo "Hexadecimal: $hexadecimal \n";
?>


Output:


Decimal: 10
Octal: 13
Hexadecimal: 34

PHP Float

The float data type is also known as double data type; it contains fractional or decimal point numbers. It can also be a negative or positive number. It requires at least a single decimal point to be a Float data type. In PHP there is no problem while adding a float number with an integer number.


<?php
    $num1 = 26.754;
    $num2 = 15.6;
    $sum = $num1 + $num2;
    echo "Sum = $sum \n";
    $n1 = 16.589;
    $n2 = 15;
    $sum2 = $n1 + $n2;
    echo "Sum = $sum2";
?>

Output:


Sum = 42.354
Sum = 31.589

PHP Boolean

The Boolean data type has only two values TRUE (1) or FALSE (0). It is the simplest data type. It is mostly used in conditional statements.

Example


<?php
    // TRUE case 
    $t = TRUE;
    if($t){
        echo "This is TRUE statement";
    }
    else{
        echo "This is FALSE statement";
    }
    echo "\n";
    // FALSE case
    $f = FALSE;
    if($f){
        echo "This is TRUE statement";
    }
    else{
        echo "This is FALSE statement";
    }
?>


Output:


This is TRUE statement
This is FALSE statement

In the above example the variable “t” has the value TRUE and in the if the condition so if the block has executed and the variable “f” has the value FALSE and in the if the condition so the else block has executed.

Compound Types

Compound types are user-defined data types. Compound data types can hold multiple values. There are two compound data types which are:

  1. Array
  2. Object

PHP Array

The array is one of the compound data types in PHP. The array can store similar multiple values in a single variable. There are three types of arrays in PHP Numeric array, Associative array, and Multidimensional array. We will learn more about the array in the upcoming module.

Example


<?php
    $programing_languages = array("PHP", "Python", "Java", "C++");
    echo $programing_languages[0];
?>

Output:


PHP

PHP Object

Object and class are the most important aspects of object-oriented programing. A class is a template for objects, and an object is an instance of the class. The object is also used to store multiple values. We will learn more about objects in the upcoming module.

Example


<?php   
     class Tutorial {  
          function welcome($name) {  

               echo "Hi! $name welcome to PHP Tutorial.";  
             }  
     }  
     $obj = new Tutorial();  
     $obj -> welcome("John");  
?>  


Output:


Hi! John welcome to PHP Tutorial.

Special Types

Special data types contain special values. There are two special data types which are:

  1. NULL
  2. Resource

PHP NULL

NULL is one of the special data types. NULL data type holds only one value which is “NULL”. In PHP NULL data type is used to give variables no value. It is also used to empty the variable. The conventional way of assigning NULL is in upper case letters but technically NULL is not case sensitive.
 


<?php   
 $x = NULL;
 echo $x;
?>  

For the above example, the output will be blank because there is no value in the variable ‘x’. To see the data type is correctly assigned we should use the var_dump() function.


<?php
    $x = "hello";
    var_dump($x);
    $x = NULL;
    var_dump($x);
?>

Output:


string(5) “hello”
NULL

PHP Resource

The resource data type is one of the special data types. In PHP resource data type is not an actual data type. The resource data type is mostly used to store the external resources. Mostly the database calls. In PHP get_resource_type() is the predefined function used to return the resource type of the variable.

Example using var_dump()


<?php
$file_open = fopen("test.txt","w");
var_dump($fp);
?>


Output:


resource(5) of type (stream)

Example using get_resource_type()


<?php
$file_open = fopen("test.txt","w");
echo get_resource_type($file_open);
?>

Output:


stream

In the given example we have used the fopen function to open or create a file in the system. We will learn more about resources in the upcoming modules.