Data Types in PHP

PHP Data Types is used to store different type of value like integer, character, string, array, float, decimal, date, boolean etc. in variable.

A variable’s specific data type is very important in programming because the data type helps determine the manner in which the value is stored and how much memory the computer allocates for the data stored in the variable.

The data type also governs the kinds of operations that can be performed on a variable.

PHP supports the following data types:

  1. Integer
  2. Boolean
  3. String
  4. Float
  5. Array
  6. Object
  7. NULL
  8. Resource

Integer

Integer is numeric data with a negative or positive sign between -2,147,483,648 and 2,147,483,647.

Rules for integers:

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16- based - prefixed with 0x) or octal (8-based - prefixed with 0)
  • In the following example $x is an integer. The PHP var_dump() function returns the data type and value.

Example:

<?php
	$i = 1254;
	echo "Integer Value : ".$i;
?>

Output: Integer Value : 1254


Boolean

A Boolean represents two possible states: TRUE or FALSE. Alternatively, you can use zero to represent FALSE, and any nonzero value to represent TRUE.

Example:

<?php
	$x=TRUE;
	if ($x){
		echo "This condition is TRUE.";
	}else{
		echo "This condition is FALSE.";
	}
?>

Output: This condition is TRUE.


String

Simply put, a string is a sequence of characters treated as a contiguous group. Strings are delimited by single or double quotes.

The following are all examples of valid strings.

  • "PHP is a great language"
  • "Whoop-de-do"
  • '*9subway\n'

Example:

<?php
	$str1 = "Learn PHP";
	$str2 = 'Learn PHP';
	echo $str1."<br>";
	echo $str2;
?>

Output:

Learn PHP
Learn PHP

Float

Floating-point numbers, also referred to as floats, doubles, or real numbers, allow you to specify numbers that contain fractional parts. Floats are used to represent monetary values, weights, distances etc.

PHP’s floats can be specified in a variety of ways, several of which are demonstrated here:

  • 4.5678
  • 4.0
  • 8.7e4
  • 1.23E+11

Example:

<?php
	$i = 10.365;
	$j = 2.456;
	$num = $i/$j;
	echo "Division of floating numbers: " .$num;
?>

Output: Division of floating numbers: 4.2202768729642


Array

An Array is a variable which holds collection of homogeneous values. In the following example $color is an array. The PHP var_dump() function returns the data type and value.

Example:

<?php
	$color = array("black","pink","red","green","white");
	var_dump($color);
?>

Output:

array(5) { 
	[0]=> string(5) "black" 
	[1]=> string(4) "pink" 
	[2]=> string(3) "red" 
	[3]=> string(5) "green" 
	[4]=> string(5) "white" 
}

Object

An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared.

First class of object must be declared. For this, use the class keyword. A class is a structure that can contain properties and methods.

Example:

<?php
 	class Mobile {
 		function model() {
 			$model_name = "Samsung";
 			echo "Mobile Model: " .$model_name;
 		}
 	}
 	$obj = new Mobile();
 	$obj->model();
?>

Output: Mobile Model: Samsung


NULL

Null is a special data type which can have only one value NULL. A variable of data type NULL is a variable that has no value assigned to it.

Note: If a variable is created without a value, it is automatically assigned a value of NULL.

Example:

<?php
	$p = NULL;
	var_dump($p);
	echo $p; // it doesn't show any value;
?>

Output: NULL