Data Types in PHP with Examples

Data types are one of the most fundamental concepts in PHP, as well as in any other programming language. They define how the language stores the different kinds of data.

When you write a program in PHP, you may need to store different types of data in variables. A data type defines the type of value or data that you store in a variable.

Data types are used to create the variables, which can store data of different types, ranging from simple string and numeric types to more complex arrays and objects.

Note that PHP is a loosely or weakly typed language, meaning you do not need to explicitly declare the data type of a variable when you create it. PHP interpreter automatically determines the data type of variable based on the data or the value assigned to it.

In other words, PHP automatically converts the variable to the correct data type based on its value. For example, you can assign a string to a variable and later assign an integer to the same variable without any issues. For example:

<?php
$variable = "Hello World!"; // Initially a string
echo $variable;
echo "<br>";
$variable = 42; // Now an integer
echo $variable;
?>
Output:
       Hello World!
       42

PHP Data Types


Like other programming languages, PHP also provides a wide range of data types for storing and manipulating data or value. Each data type has its own specific characteristics and properties that allow you to handle different types of data in the program.

PHP supports eight types of primitive data types, which are as follows:

  • String
  • Integer
  • Float (floating point numbers – also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource


We classify these eight primitive data types into three different categories. They are:

  1. Scalar data types
  2. Compound data types
  3. Special data types

PHP data types: Scalar, Compound, and Special data types.

Scalar Data Types


Scalar data types are those data types in PHP which are used to represent a single value. They are also known as base data types because they offers the base for other data types such as array, object, etc.

There are four kinds of scalar data types available in PHP, which are as follows:

  • String
  • Integer
  • Float
  • Boolean

PHP String Data Type


In PHP, or other programming languages, a string is a sequence of characters, like “Learn PHP”. It can be any text written inside quotation marks. You can use single or double quotes to store string values. Let’s take an example in which we will display the output text in the browser by using string data type.

Example 1:

<?php
$stringVar = "Hello, World!";
echo $stringVar;
?>
Output:
      Hello, World!

In this example, we have declared a variable named $stringVar which is holding a string type data “Hello, World!”. We have written this string type data inside the double quotes. Then, we have used the echo statement to display the value of $stringVar in the browser.

Example 2:

<?php
$str1 = "Hello User!";
$str2 = "This is a string value.";
echo $str1;
echo "<br>";
echo $str2;
?>
Output:
      Hello User!
      This is a string value.

In this example, we have declared two variables named $str1 and $str2. They are holding a string type data: “Hello User!” and “This is a string value.” We have written both strings inside the double quotes.


Then, we have used the echo statement to display the values of $str1 and $str2 on the console. As you will see in the code, we have used a statement echo “<br>”;. Here, <br> tag inserts a line break in the HTML document.

The <br> tag is especially for HTML, which differs from “\n”. In programming language, “\n” is a special character sequence used to represent a newline. It is used for newlines in plain text or console. If you will use it at the place of <br>, the same output will appear on the console, but not in HTML webpage.

Note that before PHP 7, there were some restrictions in the length of the string. But now, in PHP 7 or onwards, there are no such restrictions regarding the length of string on 64-bit system.

PHP Integer Data Type


The integer data type represents a non-decimal number between -2,147,483,648 and 2,147,483,647 on a 32-bit system, and a larger range of values on a 64-bit system. You can use the constant PHP_INT_MAX to determine the max value.

<?php
echo PHP_INT_MAX;
?>

A non-decimal number is the whole number, which does not contain any fractional part or decimal point. For example, -5, 0, 25, etc. Rules for integer data type are as follows:

  • An integer data must have at least one digit
  • An integer data must not have a decimal point
  • It can be either positive or negative.
  • You can specify integers in four formats: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation.

Example 3:

<?php
$intVar = 1255;
$strVar = "This is an integer value.";
echo $intVar;
echo "<br>";
echo $strVar;
?>
Output:
      1255
      This is an integer value.

In this example, we have assigned an integer data type value 1255 to a variable named $intvar. Then, we have assigned a string type value to a variable named $strVar. The echo statement is used to display the values of variables named $intVar and $strVar.

Example 4:

<?php
$a = 42; // Decimal (10-based)
$b = 0755; // Octal (8-based prefixed with 0)
$c = 0xC4E; // Hexadecimal (16-based prefixed with 0x)

echo $a;
echo "<br>";
echo $b;
echo "<br>";
echo $c
?>
Output:
       42
       493
       3150

PHP Float Data Type


The float data type in PHP is used to represent a floating point number that contains decimal point or real numbers with a fractional part. All float, double, or real numbers are the part of float data type. You can assign floating-point numbers or values to variables using decimal notation, positive and negative.


Example 5:

<?php
$a = 25.25; // A floating point number.
$b = -25.25; // A negative floating point number.
echo $a;
echo "<br>";
echo $b;
?>
Output:
       25.25
      -25.25

In this example, $a and $b are the variables in PHP and 25.25 and -25.25 are the float type values assigned to them.

PHP Boolean


A Boolean represents two logical values: true or false. Alternatively, you can use false to represent zero value and true to represent any nonzero value. Boolean are often used in decision-making statements, such as conditional expressions, or to control the flow of a program.

Example 6:

<?php
$a = true;
$b = false;
echo $a;
echo "<br>";
echo $b;
?>
Output:
      1

In this example, $a is a variable and true is the boolean type value assigned to it. Similarly, $b is a variable and false is the boolean type value assigned. Note that you can directly assign boolean values to variables.

Compound Data Types


Compound data types in PHP are used to store more than one value of the same type under a single representative entity. PHP supports two types of compound data types. They are as follows:

  • Array
  • Object

PHP Array


An array in PHP is a collection of data elements that can be of the same or different types. It is used to store multiple values in a single variable. PHP arrays can use numeric or associative keys to specify particular data elements. In an indexed array, the indices start at 0 and go up to n – 1, where n is the total number of elements.

Example 7: (Indexed Array)

<?php
$arrayVar = array(10, 20, 30);
echo $arrayVar[0];
echo "<br>";
echo $arrayVar[1];
echo "<br>";
echo $arrayVar[2];
?>
Output:
      10
      20
      30

In the above example, $arrayVar is a single variable which stores multiple values or elements. The array() is an array function used to define data elements of the array inside the parentheses.

Here, 10, 20, and 30 are the data elements of the array referred by the common name $arrayVar. $arrayVar[0], $arrayVar[1]. and $arrayVar[3] are the array index, which will display the array elements according to the index.

Example 8: Associative Array

<?php
$assocArrayVar = array("key1" => "value1", "key2" => "value2");
echo $assocArrayVar["key1"];
?>
Output:
     value1

PHP Object


An object is a compound data type in PHP that stores both data and functions. It is an instance of a class, which acts as a template for creating objects.

An object is created from a class using the new keyword. Every object has properties and methods corresponding to those of its class. Each object instance is independent, allowing you to manipulate objects separately, even if they belong to the same class.

Example 9:

<?php
class Car {
   public $make;
   public $model;

// Constructor declaration.
public function __construct($make, $model) {
   $this->make = $make;
   $this->model = $model;
}
// Method declaration.
public function display() {
   return "Car: $this->make $this->model";
 }
}
// Creating an instance of the class Car. 
$car = new Car("Toyota", "Corolla");
echo $car->display();
?>
Output:
       Car: Toyota Corolla

In this example, we have defined a class named Car. This class has two public properties: $make and $model. Then, we have created a constructor that is automatically called during the object creation.

This constructor takes two parameters, $make and $model, and assigns them to the corresponding properties of the class using $this->make = $make; and $this->model = $model;. The $this keyword refers to the current instance of the class.

After that, we have declared display() function that returns a string. It concatenates the values of the $make and $model properties to create a string representation of the car. The method uses $this->make and $this->model to access the current object’s properties.

At last, we have created an instance of Car class using new keyword and passed two argument values “Toyota” and “Corolla” to its constructor. The echo $car->display(); statement calls the display() method using the $car object reference and prints the returned string, “Car: Toyota Corolla”, to the screen.

Special Data Types


There are two types of special data types in PHP. They are as follows:

  • NULL
  • Resource

These two data types are used in the special cases in PHP programming language. Let’s understand them.

PHP NULL


NULL is a special data type in PHP that has only one value NULL. A null value represents a variable with no value. If you create a variable without any value, the PHP interpreter automatically assigns the NULL value to it. You can also empty a variable by setting its value NULL.

Example 10: 

<?php 
$lang = "PHP";
$lang = NULL;
echo $lang;
?> 

In this example, “PHP” is a string type value assigned to the variable named $lang. NULL is the NULL type value assigned to the $lang variable. The echo statement will display the value of $lang assigned to it on the browser. But there will be no output in the browser because the value of $lang variable has become null (empty).

PHP Resource


A special resource type is not an actual data type in PHP. Actually, it is a special variable which stores the reference to access an external resource such as a PHP file. The special functions create and use these resources. Database calls or establishing database connections are the most typical examples of using the resource data type.

You will learn more about the resource type in later tutorial.

Getting the Data Type


PHP provides a var_dump() function that you can use to get the type of any object which is passed to it. The general syntax of this function is as follows:

Void var_dump(variable_name);

You can use this function to determine the type of a variable. Let’s take an example on it.

Example 11:

<?php
$intVar = 10;
$bool = true;
$largeInt = 12345678901234567;
$str = "Hello";

// Getting the type of variables.
var_dump($intVar);
var_dump($bool);
var_dump($largeInt);
var_dump($str);
?>
Output:
      int(10)
      bool(true)
      int(12345678901234567)
      string(5) "Hello"

Change Data Type


If you assign an integer value to a variable, PHP will automatically convert the type of variable to be an integer. Again, if you assign a string value to the same variable, PHP will automatically change the type of variable to be string.

Example 12:

<?php
$x = 5; // Initially, an integer.
var_dump($x);

// Now assigning the string value to the same variable $x.
$x = "Hello";
var_dump($x);
?>
Output:
     int(5)
     string(5) "Hello"

Type Casting


Like other programming languages such as Java, PHP allows type casting to convert a value from one data type to another. Look at the below example.

Example 13:

$intVar = 10;
$floatVar = (float)$intVar; // type casting
echo $floatVar; // Output: 10.0
Output:  
      10.0

You will learn more about type casting in PHP casting tutorial.


Key Points of PHP Data Types

Here are the key points summarizing PHP data types:

Scalar Types:

  • Integer: Represents whole numbers.
  • Float: Represents numbers with decimal points.
  • String: Represents a sequence of characters.
  • Boolean: Represents truth values (true or false).

Compound Types:

  • Array: Stores multiple values in a single variable, accessible via indices or keys.
  • Object: An instance of a class that can hold both data (properties) and functions (methods).

Special Types:

  • Resource: Holds references to external resources, like files or database connections.
  • NULL: Represents a variable with no value or an empty variable.