PHP Data Types


The Datatype defines the type of data a variable can store. The values assigned to a PHP variable may be of different types including simple string and numeric types to more complex data types like arrays and objects. As we know that the PHP is a loosely typed language, it does not have explicit defined data types. PHP determines the data types by analysing the attributes of supplied data.

PHP Supports eight primitive data types, which are as follows :

  • Integer
  • String
  • Boolean
  • Double (also called float or floating point numbers)
  • Null
  • Array
  • Object
  • Resource

1. Integer :

Integers are whole numbers without a decimal points. An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. An integer must have at least on digit and be either positive or negative. Integers can be specified in decimal (base 10), hexadecimal (base 16 - prefixed with 0x) or octal (base 8 - prefixed with 0) notation.

Example :
<?php
  $x = 256; // decimal
  var_dump($x);
  echo "<br/>";

  $x = 0xEE; // hexadecimal
  var_dump($x);
  echo "<br/>";

  $x = 0076; // Octal
  var_dump($x);
  echo "<br/>";

  $x = -500; // negative number
  var_dump($x);
?>
Output :

int(256)
int(238)
int(62)
int(-500)

At the above example we used var_dump() php function which returns the data type and value of supplied variable.


2. String :

String is a sequence of characters, which are written within double quotes or single quotes during declaration. A string can hold letters numbers and special characters.

Example :
<?php
  $x = "Hello world";
  echo $x;
  echo "<br/>";
  var_dump($x);
  echo "<br/><br/>";
 
  $y = 'PHP Strings';
  echo $y;
  echo "<br/>";
  var_dump($y); 
?>
Output :

Hello world
string(11) "Hello world"

PHP Strings
string(11) "PHP Strings"


3. Boolean :

A Boolean represents two possible values, either TRUE (1) or FALSE (0). It is often used in the conditional testing.

Example :
<?php
  $x = TRUE;
  var_dump($x);
  echo "<br/>";
 
  $y = FALSE;;
  var_dump($y); 
?>
Output :

bool(true)
bool(false)


4. Double (float) :

A double or float or floating point number is a number with a decimal point. It could also be decimal, fractional number or a number in exponential form.

Example :
<?php
  $x = 25.78;
  var_dump($x);
  echo '<br/>';

  $y = 5.5e3;
  var_dump($y);
  echo '<br/>';

  $z = 3E-2;
  var_dump($z);
?>
Output :

float(25.78)
float(5500)
float(0.03)


5. Null :

The Null is a special data type which can have only one value : NULL. A variable of type NULL is a variable without any data. If a variable is created without a value, it is automatically assigned a value of NULL. We can also assign a variable to NULL.

Example :
<?php
  $x = null;
  var_dump($x);
  echo '<br/>';

  $y = "PHP Test String.";
  $y = null;
  var_dump($y);
?>
Output :

NULL
NULL


6. Array :

In simple words an array is a special variable, which can hold more than one value at a time. An array is a collection of values that are belonging to a same data type. Each value is an element of the array and has an associated index number. We can refer to a particular element in the array by using the name of the array and the index number. The index number starts at zero.

Example :
<?php
  $webl = array("HTML", "CSS", "JavaScript", "PHP", "Mysql");
  var_dump($webl);
  echo "<br/><br/>";

  echo $webl[0];
  echo "<br/>";
  echo $webl[1];
  echo "<br/>";
  echo $webl[2];
  echo "<br/>";
  echo $webl[3];
  echo "<br/>";
  echo $webl[4];
  echo "<br/>";
?>
Output :

array(5) { [0]=> string(4) "HTML" [1]=> string(3) "CSS" [2]=> string(10) "Javascript" [3]=> string(3) "PHP" [4]=> string(5) "Mysql" }

HTML
CSS
JavaScript
PHP
Mysql


7. Object :

An object is a data type that not only allows storing data but also information on, how to process that data. Objects are instances of programmer-defined classes, which can package up both values and functions that are specific to the class. In PHP,an object must be explicitly declared. First we must declare a class of object, a class is a structure that contain properties (values) and methods (functions).

Example :
<?php
  class Banner {
    public $str1 = "Hello world.";
    public $str2 = "This is PHP Tutorials.";

    function show_banner() {
      echo $this->str1 . "<br/>" . $this->str2;
    }
  }

  $display = new Banner;
  $display->show_banner();
  echo "<br/><br/>";
  var_dump($display);
?>
Output :

Hello world.
This is PHP Tutorials.

object(Banner)#1 (2) { ["str1"]=> string(12) "Hello world." ["str2"]=> string(22) "This is PHP Tutorials." }


8. Resource :

The special resource type is not an actual data type. Resource variables typically hold special handlers to opened files and database connections.


Next Topic :