Overview of PHP Data Types

A Data Type is a type of data. The values that is assigned to the variables are of different data types that includes string, numeric types to array and objects type.

PHP supports eight primitive data types. They are

  • Integer
  • Float
  • String
  • Booleans
  • Array
  • Object
  • Resource
  • NULL

PHP Integers

  • Integers are whole numbers.
  • They do not have decimal points.
  • It includes positive and negative numbers both.
  • We can specify integers in decimal (base 10), hexadecimal (base 16 – prefixed with 0x) or octal (base 8 – prefixed with 0)

Example:-

<?php
	//decimal base integers
	$dec1 = 50;
	$dec2 = 80;
	//octal base integers
	$oct = 07;
	//hexadecimal base integers
	$hex = 0x45;
	$hex = 0x45;
	$sum = $dec1 + $dec2;
	echo $sum;
?>

Output:-

130

PHP Floating point numbers

  • Floating point numbers also known as float or double or real numbers.
  • This are the decimal points or fractional numbers including positive and negative numbers.

Example:-

<?php
	$a = 10.85;

	$b = 100.28;

	$sum = $a + $b;

	echo $sum;
?>

Output:-

111.13

PHP Strings

  • A String is a collection of characters.
  • It contains alphanumeric characters i.e. it can hold any alphabets as well as numbers
  • Strings can be written in double quotes as well as in single quotes but it will be treated differently on execution.

Example:-

<?php 
	$a = "Mark"; 
	echo "Hello $a \n"; 
	echo 'Hello $a'; 
?>

Output:-

Hello Mark

Hello $a

PHP Booleans

  • Boolean holds only two values either true or false.

Example:-

<?php
	$flag = TRUE;
	if($flag)
		echo "This condition is TRUE";
	else
		echo "This condition is FALSE";
?>

Output:-

This condition is TRUE

PHP Arrays

  • An Array is the data type which can hold several values of same data type at a time.
  • The data values in the array are indexed.

Example:-

<?php
	$a = array( 10, 20 , 30);

	echo "First Number: $a[0]\n";

	echo "Second Number: $a[1]\n";

	echo "Third Number: $a[2]\n";
?>

Output:-

First Number: 10

Second Number: 20

Third Number: 30

PHP Objects

An Object is a data type which holds both value and functions. It also stores data and provide information on how to process the data. Instances of every object is independent of its own methods and properties and hence can be manipulated.

PHP Resources

Resources are not exactly data types but they basically store references to the external resources. For example, to open files or to make database call.

PHP NULL

  • NULL is the special variable that hold only NULL value that means empty variable.
  • The NULL variable type doesn’t contain any data.
  • It is case sensitive.

Example:-

<?php
	$n = NULL;
	
	echo $n;                      // This will give no output
?>