PHP data type allows to store different kind of data into a variable and specifies what type of operation can be performed. PHP has following data type.

  • Pre-defined data types
    • integer
    • float
    • string
    • boolean
  • Compound or user-defined data type
    • array
    • objects
  • Special data types
    • null
    • resource

integer

Whole positive and negative numbers within a range of -2^31 to 2^31 ( -2,147,483,648 and 2,147,483,647.). PHP Default base of integers is base 10,

Octal and Hexadecimal base integers are also allowed. To use Octal, prefix 0 before number without space and for hexadecimal number use 0x before number.

integer0, 1, 22, -23, 90
octal integer02, 08, 044
Hexadecimal integer0x30, 0x50
<?php
  // Integer
  $a = 20;
  $b = -30;

  // octal Integer
  $c = 09;

  // Hexadecimal Integer
  $d = 0x44;
?>

Float (Double)

In PHP, float data types allows fractional part after number.

<?php
  // float
  $a = 20.55;
  $b = -30.45;
?>

String

String is a sequence of characters, numbers or symbol. In PHP, string should be inside double quotes or single quote.

<?php
  // String inside double quote
  $a = "Hello World";
  // String inside single quote
  $b = 'Hello World';
?>

Boolean

Boolean has only two values TRUE or FALSE.

<?php
  $a = TRUE;
  $b = FALSE;
  $c = true;
  $d = false;
?>

Boolean values are case-insensitive, So, capital or Small case does not matters, But it should not be inside double quotes or single quote, In such case, PHP treats as string.

 <?php
  $a = "TRUE";
  $b = "FALSE";
  $c = "true";
  $d = "false";
  var_dump($a);
  var_dump($b);
  var_dump($c);
  var_dump($d);
?>
Outputs
string(4) "TRUE" 
string(5) "FALSE" 
string(4) "true" 
string(5) "false"

Array

<?php

$arr = array( 101, 120, 310, 450, 340);
 echo $arr[0];
 echo $arr[1];
 echo $arr[2];
 echo $arr[3];
 echo $arr[4];

 var_dump($arr);

?>

Objects

In PHP, To use class properties and methods, objects are created at runtime using new operator.

<?php
 class Phone{
  public $company;
  public $color;
  public function __construct($company, $color) {
    $this->company = $company;
    $this->color = $color;
  }
  public function getPhoneDetails() {
    echo "Company:" . $this->company . " color is: " . $this->color;
  }
 }

$myPhone = new Phone("Samsung", "A14");
$myPhone->getPhoneDetails();
?>

PHP NULL Value

NULL value is absense of value or no value.  NULL is case-insensitive i.e. NULL, null, Null all are same.
<?php
$a = null;
echo $a;
?>

Resource

It is used to hold external references of database connection and function calls. It is not a data type.