New way to learn PHP

PHP 5 Data types

Data type

Most of other languages like C/C++ and Java require specifying what kind of data it can hold, but in PHP there is no need to specifying data type before using it. All the data store in PHP variable. Data types are the backbone of any programming language.

Example of data type


echo "First value=".$test."
"; echo "Second value=".$test1."
"; echo "Result=".$sum; ?>

In this example $test, $test1 are the float type variable, after multiplying the $test and $test1 store the value into $sum variable. You can use gettype function to check the data type of the variable.

Data types in PHP under various categories are shown below

  • String
  • Integer
  • Floating point numbers
  • Boolean
  • Array
  • Object
  • NULL

Hierarchy of data type

  1. Scalar Data Type
  2. Compound Data Type
  3. Special Data Type
Data types

Scalar Data Type

  • Scalar data means data that contains only a single value.
  • There are four scalar data types.

All are scalar data types are following below.

Scalar data type Description Example
Integers A whole number $a =38;
Float A floating point number $a=5.50;
Strings Sequence of character $a="string";
Boolean Either true or false $a=true;


An integer is an integral whole number without a decimal point. Integer numbers are used for counting purposes, like 38, 8 etc.


Float variables hold Fractional, like 38.025, 7.652 etc. It's used for banking and financial sector calculation.


A string is defined as a character array, like John, car etc. It's used for storing the array of character.


Boolean variables hold true or false.

All of scalar data type illustrate with example below


$test = 7;
$test1 = 38;
$sum = $test+$test1;
echo "First value=".$test."
"; echo "Second value=".$test."
"; echo "sum=".$sum."
"; $test2 = "john "; $test3 = "maclen"; echo "$test2"."$test3"."
"; $a = 35.25; $b = 65.32; $diff = $b-$a; echo "difference=".$diff; ?>

Example of boolean data type


echo " value of variable a is ture";	
echo"value of variable a is not ture";	



value of variable a is ture

In the above example $a is not a string variable, it's a boolean variable and hold the value "true". So that the result will be "value of variable a is true".

Compound data types

PHP supports two compound types, Compound data is data that can contain more than one value.

All Scalar Data Types listed below

Compound data type Description
Array Its holds multiple values
Object A type that may contain properties and methods

Compound data type example

$numbers = array( 5,8,45,6);
foreach( $numbers as $value )
  echo "Value is $value 
"; } $numbers[0] = "John"; $numbers[1] = "apple"; $numbers[2] = "car"; foreach( $numbers as $value ) { echo "Value is $value
"; } ?>

In this example $number is an array variable that holds some integer value, you can access all the value of an array by using a foreach loop as shown above.

Special Data types

Null Data Type

  • It may contain null as a value.
  • It is a special data type.

if (!$Var)  
echo "Welcome to the  php
"; echo "Welcome to the php"; } ?>

NULL is not a string it is a special data type that means variable does not have any value, you can check by using if-else.

Check the type of variable

You can use the gettype() function to check the data type of variable, gettype function returns the data type as a string.



Illustrate with example


$test = array( 5,"umar",45.2,NULL);

foreach( $test as $value )

  echo gettype($value)."
"; } ?>

In this example, $test is an array variable that holds the integer, string, double and NULL values respectively. You can use a foreach loop to access array element one by one, pass the $value as an argument in the gettype function and it returns the data type of each element of the array.

Scope of variables

Each variable has an area in which it exists, knows as it is scope

There are two types of scope

  1. Local scope
  2. Global scope

local scope

The function has their own scope, that means the variable used in within a function not available outside the function.


Function concat() 
$a = "Hello , ";
$b = "welcome to php!";
return $a . $b;
echo concat();
/*$a cannot used any 
Where out of this function


The variable $a or $b not accessible outside the function concat, it's limited in the in that function, so it's called local variables.

Global variable

All variable set outside the function considered as global that means they can access from anywhere in the script.


$global = "Welcome to php"."
"; //its global variable function hello() { global $global; echo "$global "."
"; } hello(); // Displays "Welcome to php" echo $global; //its accessible anywhere in PHP script echo $global; hello(); hello(); ?>

Global variable accessible anywhere in PHP script using the global keyword.

Static keyword

  • A static variable will not erase its value when the function terminates.
  • It keeps the previous store value, used if the function again called.

Non static variable

echo "Non static variable example";
function cout() {
$count = 0;
echo "
".$count; } cout(); cout(); cout(); ?>


Non static variable example

In case of non-static variable previous value will lose and initialised with zero again every time count function called.

Example of static variable


function counta()
STATIC $a = 1;
return ++$a;
echo "count: " . counta() . " 
"; echo " count: " . counta() . "
"; echo " count: " . counta() . "
"; ?>

In case of static variable previous value never lost whenever function terminates. It keeps previous value.


We live in this world when we love it.