Data Types In PHP

The purpose of this article is to examine the different PHP data types with examples in more detail.

PHP is a dynamically typed language, so when you declare a variable, you do not have to specify its data type.

Based on the value assigned to a variable, PHP automatically determines the type of data that is stored there.

PHP Data Types

In order to store data, variables can be divided into different PHP data types, and each type of data can be used in a different way.

As far as PHP Data types are concerned, there are several types available:

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

PHP Data Type – String

A string consists of a sequence of characters. For example, a string could be “Elon Musk ”.

In a string, you can put any text between quotes.

Strings can be enclosed in either single quotes () or double quotes ().

To declare a string variable, simply assign a string value to it. For example:


<?php $a = "Elon"; $b = 'Musk'; echo $a; echo " "; echo $b; ?>


<?php $a = "Good morning"; echo $a; ?>

PHP Data Type – Integer

An integer is a whole number, either positive or negative, without a fractional component.

In PHP, integers can range from -2,147,483,648 to 2,147,483,647.

Integer rules:

  1. One digit is required for an integer.
  2. No decimal point in an integer.
  3. Integers can be either positive or negative.
  4. Binary (base 2), octal (base 8), decimal (base 10), and hexadecimal (base 16) notation can be used to specify integers.

An integer is represented by $a in the following example. Data type and value are returned by PHP’s var_dump() function:


<?php $a = 487; var_dump($a); ?>


<?php $b = 1054; var_dump($b); ?>

PHP Data Types – Float

The term float refers to a number with a decimal point or an exponential number.

In PHP, floating-point numbers are stored as double-precision floating-point values.

To declare a float variable, simply assign a number with a fractional component to it.

As an example, $a is a float. PHP’s var_dump() function returns the data type and value:


<?php $a = 21.63; var_dump($a); ?>


<?php $b = 12.124; var_dump($b); ?>


PHP Data Types – Boolean

There are only two possible values for boolean data types: true or false.

Booleans are commonly used in PHP conditional statements to determine whether a condition is true or false.

The only way to declare a boolean variable is to assign it the value TRUE or FALSE. Here are some examples:

$a = true;
$b = false;

PHP Data Types – Array

In PHP Data types, An array is a data structure that can store multiple values, each accessible by a unique key.

In PHP, arrays can be indexed, associative, or a combination of both.

To declare an array, use the array keyword, or use square brackets ([]).

Here, $flowers is an array. PHP var_dump() returns the data type and value:


<?php $flowers = array("Sunflower","Rose","Jasmine"); var_dump($flowers); ?>

Later in this tutorial, you will learn more about arrays.

PHP Data Types – Object

There are two main aspects of object-oriented programming that are classes and objects. Objects are data types that represent instances of classes.

PHP objects are used to store data and behavior (methods) associated with particular entities.

Class definitions can be defined as templates for defining objects, and objects can be defined as instances of classes.

Each object that is created inherits all the properties and behaviors that are found in the class. However, each object will have its own unique set of values for those properties in order to be unique.

Let us assume for a moment that we have a class named Flower.

The properties of a flower can include things like the type, the color, etc. For instance, we can define variables named $type, $color, etc., to hold values of these properties in the form of their values.

The individual objects (Sunflower, Rose, Jasmine, etc.) inherit all the properties and behaviors from the class when they are created, although the values of the properties will be different for every individual object.

In PHP, if you create a __construct() function, the function will automatically be called by PHP whenever an object is created from a class that uses the __construct() function.


<?php class Flower{ public $color; public $type; public function __construct($color, $type) { $this->color = $color; $this->type = $type; } public function message() { return "Flower color is " . $this->color . " and type is " . $this->type . "!"; } } $myFlower = new Flower("red", "Rose"); echo $myFlower -> message(); echo ""; $myFlower = new Flower("yellow", "SunFlower"); echo $myFlower -> message(); ?>


<?php class Pet{ public $name; public $weight; public function __construct($name, $weight) { $this->name = $name; $this->weight = $weight; } public function message() { return "Pet name is " . $this->name . " and weight is " . $this->weight . "kg!"; } } $myPet = new Pet("Dog", "20"); echo $myPet -> message(); echo ""; $myPet = new Pet("Cat", "18"); echo $myPet -> message(); ?>

Example Explanation:

In above example we have defines a class called “Pet”.

The class has two public properties, “name” and “weight“, and a constructor method that sets the values of these properties when an object of the class is created.

The class also has a method called “message” which returns a string that combines the values of the “name” and “weight” properties.

Finally, two objects of the “Pet” class are created and their “message” method is called, resulting in two different strings being printed.

The first object is created with the values “Dog” and “20” for the “name” and “weight” properties, respectively.

The second object is created with the values “Cat” and “18“.


PHP Data Types – NULL Value

The NULL value is a special data type in PHP that represents an absence of a value.

In other words, it is a value that indicates that a variable has been declared, but has not been assigned a value.

The NULL value is a unique value that is different from other data types, including an empty string or an integer with a value of 0.

HINT: During the initialization process of a variable, if it is not given a value, it will automatically be assigned a value of NULL.

The value of a variable can also be emptied by setting its value to NULL, as follows:


<?php $a = "Elon Musk"; $a = null; var_dump($a); ?>

In PHP, the NULL value can be useful in a number of situations.

For example, it can be used to initialize variables that will be later assigned a value.

It can also be used to represent missing or unknown values, or to indicate that a variable no longer holds a valid value.

To check if a variable has a value of NULL, you can use the is_null() function as follow:


<?php $b = 80; $b = null; if (is_null($b)) { echo "The variable \$b is NULL."; }?>


<?php $b = 80; $b = null; var_dump($b); ?>

PHP Data Types – Resource

There is no actual data type associated with the special resource type. References to external functions and resources are stored here.

Database calls are a common example of when you would use the resource data type.

Since this is a topic that is more advanced, we will not talk about resource types here.


We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *