PHP Data Types

Data type is the type of the data a variable is storing. PHP has different datatypes mentioned as follows –

Datatypes in PHP can be broadly categorized in three categories –

Scalar types

They can hold only one value at a time. They further are of four types –

  • Integer
  • Float
  • String
  • Boolean

Compound Types

They can hold multiple values at a time.

  • Array
  • Object

Special type

They have some special purposes

  • NULL
  • Resource

Scalar types 

  • Integer :  An integer is a numeric value with no decimal value. Like 2, 4 ,7 etc. It has a range from -2,147,483,648 to 2,147,483,647.

For example –

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

Output:

7

If you need the details regarding a particular value type, use can use the inbuilt  function var_dump().

<?php
    $x = 7;
    var_dump($x);
?>

Output:

int(7)

  • Float : A Float or floating Point Number is a numeric value with decimal point. In the below example the value as well as the data type of the variable can be printed as –
<?php
    $x = 7.4;
    var_dump($x);
?>

Output:

float(7.4)

  • String : In PHP, and in most programming and scripting languages, a string is a group of characters. For example ‘mango1’. A string should always be enclosed in quotes (single or double).
<?php
    $city = "Sagar";
    echo $city;
?>

Output:

Sagar

In above example $city is a variable containing a string value ‘Sagar’.

  • Boolean: A boolean value can be either true or false, nothing else!
<?php
    $a = true;
    $b = false;
?>

A boolean value is generally used in conditional statements. A conditional statement executes only if the boolean value of the specified expression is true.

For example –

<?php
    $a = 4;
    if($a < 10)
    {
        echo "The number is less than 10";
    }
?>

Since the value inside the variable ‘a’ is less than 10 (4 is less than 10), the boolean value of the entire expression ‘$a < 10’ is true, and hence the message would be displayed.

Output :

The number is less than 10

Compound types

  • Array : An array is a special type of variable that can store multiple values at a time.
<?php
    $fruits = array("Mango", "Apple", "Banana", "Papaya");
    echo $fruits[0];
?>

Output:

Mango

Here $fruits is an array containing four string values. An array in PHP can be declared by using the inbuilt array() function

Array elements can be fetched with the help of index numbers which always starts from 0. So “Mango” will have index number 0, Apple have index 1, Banana have 2 and Papaya have 3.

Note : The index number of the element is always one less than total number of elements in the array.

You will learn about arrays in deep in later tutorials.

  • Object : This data types relates to object oriented programming (OOP). In OOP, class is a blueprint for objects, and an object acts as an instance of a class.
<?php
    class Person {
        public $username;
        public $age;
        public function __construct($username, $age) {
            $this->username = $username;
            $this->age = $age;
        }
        public function outputMessage() {
            return "Name is  " . $this->username . " and age is " . $this->age . ".";
        }
    }

    $userone = new Person("Varun", 22);
    echo $userone -> outputMessage();
    echo "<br>";
    $usertwo = new Person("Aman", 24);
    echo $usertwo -> message();
?>

Output :

Name is Varun and age is 22.
Name is Aman and age is 24.

In above example, the class is Person, and userone and usertwo are two individual objects of class “Person”.

Special Type

  • NULL : NULL is a special datatype that can only have a single value – NULL. In PHP, a variable cannot be declared without having a value assigned, therefore if you want a variable to have nothing, a NULL value can do the job. A variable containing a NULL value can be thoought of containing no value at all.
<?php
    $a = NULL;
    var_dump($a);
?>

Output:

NULL

  • Resource: There is another datyatype in PHP named “Resource”. But it is a very advanced topic, we shall cover it in later tutorial.