PHP Data Types

PHP has built-in Data Types which is divided into three catagories Scalar types,Compound types and Special types. PHP has total eight (8) different data types you can work with.

Scalar types

  • boolean
  • integer
  • float
  • string

Compound types

  • array
  • object

Special types

  • resources
  • NULL

Boolean Type

In PHP the boolean data type is a primitive data type they have only two possible values true and false. This is a fundamental data type and very common in computer programs.

PHP Boolean Type Example

$isVisited = False;
$isVisited = userVisited();
if ($isVisited) {
    echo "User visited";
} else {
    echo "User did not visited";

Integer Type

Integer is a whole number. PHP integer is the same as the long data type in C. It is a number between -2,147,483,648 and +2,147,483,647. Integers can be specified in three different notations in PHP. Decimal, hexadecimal and octal. Octal values are preceded by 0, hexadecimal by 0x.

Rules for integers

  • Integer must have at least one digit (0-9)
  • Integer must not have a decimal point
  • Integer can be either positive or negative
  • Integer cannot contain comma or blanks

PHP Integer Type Example

$w = 2300;
$x = -560; // This is negative number 
$y = 0x2C; // This hexadecimal number
$z = 047; // This octal number

This will produce following result


Floating Point Numbers

Float may contain any decimal number or a number in exponential form.Floating point numbers in PHP can be larger than integers. PHP recognizes two types of floating point numbers. The first is a simple numeric literal with a decimal point and second is a floating-point number written in scientific notation.

PHP Floating Point Type Example

$var1 = 2.786;
$var2 = 2.2e3;
$var3 = 1E-10;
$var4 = 12345678923445;

This will produce following result


String Type

String is a series of single characters such as "Hello PHP". Most of working with Web pages is about working with strings. In PHP a string must be delimited by one of four syntaxes: single quotes, double quotes, heredocs, and nowdocs.

PHP String Type Example

$var1 = "My First PHP Programm!";
echo $var1;
$var1 = 'My Second PHP Programm!';
echo $var1;

This will produce following result

My First PHP Programm!
My Second PHP Programm!

Array Type

In simple word array is complex data type which hold collection of related data elements.Each of the elements can be accessed by an index.

PHP Array Type Example

$months = array("June", "July", "August", "September", "October");

This will produce following result

    [0] => June
    [1] => July
    [2] => August
    [3] => September
    [4] => October

Object Type

In simple word object are instances of class definitions. Objects are user defined data types normaly used to define properties/attributes of object. First we need to declear a class of object that can contain properties and methods.

PHP Object Type Example

class  Books{
    /* Member variables */
    var $price;
    var $title;
    /* Member functions */
    function setPrice($price_par){
       $this->price = $price_par;
    function getPrice(){
       echo "Book Price: $" .$this->price ."<br/>";
    function setTitle($title_par){
       $this->title = $title_par;
    function getTitle(){
       echo "Title: ".$this->title . "<br/>";
// creating instance of class Book
$book = new Books();

// setting properties values
$book->setTitle("PHP Tutorials Book");

// show properties

This will produce following result

Title: PHP Tutorials Book 
Book Price: $123

Resource Type

Resources are special data types. They hold a reference to an external resource (such as database connections).


NULL is a special type that only have one possible value and that's null. This type is also treated as a language constant in PHP and is case-insensitive. PHP considers all undefined variables to be of type NULL.

PHP NULL Type Example

$var="Hello world!";

This will produce following result