Data Type in PHP:

Data Type in PHP is classified into various categories according to its attributes. It defines as the type of data a variable can store.

  • Alphanumeric characters are defined as strings
  • Whole numbers are defined as integers
  • Numbers, as well as decimal points, are defined as floating points.
  • True or false values are defined as Boolean.


Integers hold only whole numbers which include positive and negative numbers that are without in fractional parts or decimal points. The integer number can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10).  whole numbers e.g. -3, 0, 22. In integer the maximum value is platform-dependent.

// decimal base integers
$deci1 = 50;
$deci2 = 654;

// octal base integers
$octal1 = 07;

// hexadecimal base integers
$octal = 0x45;

$sum = $deci1 + $deci2;
echo $sum;
echo "\n\n";


Floating point

This data type contains fractional or decimal parts including positive and negative numbers or in exponential form. The variables add a minimum number of decimal places. It is the exact as a float as floating-point numbers or real numbers.

$a = 50.85;
$b = 654.26;
$sum = $a+ $b;
echo $sum;
echo "\n\n";
//returns data type and value

705.11 float(705.11)


Boolean data types are the data types that are used in conditional testing. It holds two values: TRUE(1) or FALSE(0). The events which are successfully executed will return true and unsuccessful events return false. NULL type values are also considered false in Boolean. In this data type, 0 is also considered false. If there is an empty string then it is also considered false in the boolean data type.

    echo "This condition is TRUE";
    echo "This condition is FALSE";

This condition is TRUE


In PHP datatypes, the array is a container that can store multiple values of the same data type.  It mixes a series of data that are related together. Below is an example of an array.

$bikes = array("Hero", "Honda", "TVS");
echo "Example " . $bikes[0] . ", " . $bikes[1] . " and " . $bikes[2] . ".";

Example Volvo, BMW and Toyota.

How to Create an Array in PHP

  • In PHP, to create an array we used array().
  • EXAMPLE: array(“Hero”, “Honda”, “TVS”);
  • In PHP we consider three types of arrays:
  • Indexed arrays -In this, the arrays in which the elements are ordered based on an index. The index is also used to access or modify the elements which are in the array.

The syntax of an indexed array using the array() function is:

$newarray = array(element_1, element_2, element_3);

In PHP program:

//indexed array
$newArray = ["apple", "coconut", "orange", "yellow", "guava"];
//access element using index
$element = $newArray[3];
echo $element;


Associative arrays  –  In PHP, associative arrays are very similar to numeric arrays but they are different in terms of the index method. In an associative array, the array is named with keys.

•         Associative elements are considered in the format “key” => “value”.

•         Example:

    $marks = array("Maths"=>"99", "Science"=>"45", "HTML"=>"33");    
 echo "Obtained Marks in HTML = ".$marks['HTML']; 

Obtained Marks in HTML = 33

Multidimensional arrays

It is also called a Three Dimensional Array. This array has the same initialization as two-dimensional arrays. In another word, these arrays contain one or more arrays.

<!DOCTYPE html>

$bikes = array (
echo $bikes[0][0].": In stock: ".$bikes[0][1].", sold: ".$bikes[0][2].".<br>";
echo $bikes[1][0].": In stock: ".$bikes[1][1].", sold: ".$bikes[1][2].".<br>";
echo $bikes[2][0].": In stock: ".$bikes[2][1].", sold: ".$bikes[2][2].".<br>";
echo $bikes[3][0].": In stock: ".$bikes[3][1].", sold: ".$bikes[3][2].".<br>";

Hero: In stock: 24, sold: 155.
Honda: In stock: 15, sold: 154.
TVS: In stock: 50, sold: 234.
BMW: In stock: 3, sold: 57.

 Sort Functions In Arrays:

  • sort() – In this function, sort arrays in ascending order
  • rsort() – In this function, sort arrays in descending order
  • asort() – In this function, sort associative arrays in ascending order, according to the value
  • ksort() – In this function, sort associative arrays in ascending order, according to the key
  • arsort() – In this function,  sort associative arrays in descending order, according to the value
  • krsort() – In this function,  sort associative arrays in descending order, according to the key
<!DOCTYPE html>

$bikes = array("Hero", "Honda", "TVS");

$clength = count($bikes);
for($x = 0; $x < $clength; $x++) {
  echo $bikes[$x];
  echo "<br>";


Leave a Reply

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