Understanding the PHP Data Types | Data Types in PHP

Understanding the PHP Data Types | Data Types in PHP

The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects.

PHP supports total eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.


Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

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

You may like

1. PHP Integer

Integer : Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31.


$x = 5985;





2. PHP Strings

Strings are sequences of characters, where every character is the same as a byte

A string can hold letters, numbers, and special characters and it can be as large as up to 2GB (2147483647 bytes maximum). The simplest way to specify a string is to enclose it in single quotes (e.g. ‘Hello world!’), however you can also use double quotes (“Hello world!”).


$a = 'Hello world!';

echo $a;

echo "<br>";

$b = "Hello world!";

echo $b;

echo "<br>";

$c = 'Stay here, be back.';

echo $c;


Hello world!

Hello world!

Stay here, be back.


3. PHP Float

A float (floating point number) is a number with a decimal point or a number in exponential form.

In the following example $x is a float. The PHP var_dump() function returns the data type and value:


$a = 1.234;


echo "<br>";

$b = 10.2e3;


echo "<br>";

$c = 4E-10;








4. PHP Boolean

A Boolean represents two possible states: TRUE or FALSE.

$x = true;

$y = false;


Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.


5. PHP Array

An array stores multiple values in one single variable.

In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example a set of country or city names.

An array is formally defined as an indexed collection of data values. Each index (also known as the key) of an array is unique and references a corresponding value.


$colors = array("Red", "Green", "Blue");


echo "<br>";

$color_codes = array(

    "Red" => "#ff0000",

    "Green" => "#00ff00",

    "Blue" => "#0000ff"




array(3) {

[0]=> string(3) "Red"

[1]=> string(5) "Green"

[2]=> string(4) "Blue"


array(3) {

["Red"]=> string(7) "#ff0000"

["Green"]=> string(7) "#00ff00"

["Blue"]=> string(7) "#0000ff"


You will learn a lot more about arrays in later chapters of this tutorial.

Understanding the PHP Data Types

6. PHP Objects

Classes and objects are the two main aspects of object-oriented programming.

A class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Let’s assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.


// Class definition

class greeting{

    // properties

    public $str = "Hello World!";

    // methods

    function show_greeting(){

        return $this->str;


// Create object from class

$message = new greeting;



object(greeting)#1 (1) { ["str"]=> string(12) "Hello World!" }


7. PHP NULL Value

Null is a special data type which can have only one value: NULL.

A variable of data type NULL is a variable that has no value assigned to it.

The special NULL value is used to represent empty variables in PHP. A variable of type NULL is a variable without any data. NULL is the only possible value of type null.


$a = NULL;


echo "<br>";

$b = "Hello World!";

$b = NULL;






When a variable is created without a value in PHP like $var; it is automatically assigned a value of null. Many novice PHP developers mistakenly considered both $var1 = NULL; and $var2 = “”; are same, but this is not true. Both variables are different — the $var1 has null value while $var2 indicates no value assigned to it.


8. PHP Resource

The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.

A common example of using the resource data type is a database call.

We will not talk about the resource type here, since it is an advanced topic.

A resource is a special variable, holding a reference to an external resource.

Resource variables typically hold special handlers to opened files and database connections.


// Open a file for reading

$handle = fopen("note.txt", "r");


echo "<br>";

// Connect to MySQL database server with default setting

$link = mysqli_connect("localhost", "root", "");




resource(2) of type (stream)

Related:Understanding the PHP Data Types

You may like

Scroll to Top