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

<?php
$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

<?php
$w = 2300;
var_dump($w);
$x = -560; // This is negative number 
var_dump($x);
$y = 0x2C; // This hexadecimal number
var_dump($y);
$z = 047; // This octal number
var_dump($z);
?>

This will produce following result

int(2300) 
int(-560) 
int(44) 
int(22)

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

<?php
$var1 = 2.786;
$var2 = 2.2e3;
$var3 = 1E-10;
$var4 = 12345678923445;
var_dump($var1);
var_dump($var2);
var_dump($var3);
var_dump($var4);
?>

This will produce following result

float(2.786)
float(2200)
float(1.0E-10)
float(12345678923445)

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

<?php
$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

<?php
$months = array("June", "July", "August", "September", "October");
print_r($months);
?>

This will produce following result

Array
(
    [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

<?php
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");
$book->setPrice(123);

// show properties
$book->getTitle();
$book->getPrice(); 
?>

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 Type

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

<?php
$var="Hello world!";
$var=null;
var_dump($var);
?>

This will produce following result

NULL