Data types in PHP

Variables can store different types of data, and different data types can do different things.
Get/Set type of variable: gettype() function returns the type of the variable. settype() function is used to set type to variable. Type may change on next value assignment.

Example

<?php
    $x=10;
	echo gettype($x);              # integer
	settype($x,"string");          # sets type to string
	echo gettype($x);              # string
?>

PHP String

String is a series of characters. Strings could be single quoted or double quoted. Variables and escape sequences are not interpreted in single quoted strings. Variables and escape sequences are interpreted in double quoted strings

<?php
    $name="Hi\nHow are you";     
	echo 'welcome\nJay';           # \n is not interpreted
	echo "welcome\nJay";          # \n is interpreted
	$name='Hi\nHow are you';      
	echo $name;                        # \n is not interpreted
	$name="Hi\nHow are you";
	echo $name;                       # \n is interpreted
?>

PHP Boolean

Booleans are the simplest data types that act like switches. It only takes two values: TRUE (1) or FALSE (0). It is often used with conditional statements. It returns TRUE if the condition is true, otherwise FALSE.

<?php
	 if (TRUE)  
        echo "This condition is TRUE.";  
     if (FALSE)  
        echo "This condition is FALSE.";  
?>

PHP Integer

Integers are only whole numbers that include positive and negative numbers, that is, numbers without a fractional part or a decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is Decimal (base 10).

<?php
	// decimal base integers
	$deci1 = 50; 
	$deci2 = 654; 
	  
	// octal base integers
	$octal1 = 07; 
	  
	// hexadecimal base integers
	$octal = 0x45; 
	  
	$sum = $deci1 + $deci2;
	echo $sum;
  
?>

PHP Float

A float (floating point number) is a number with one decimal point or a number in exponential form.

<?php
	$x = 10.365;
	var_dump($x);
?>

PHP Array

An array is a compound data type. It can store multiple values of same data type in a single variable. A collection of key/value pairs. A key/index can be integer or string. Keys can be non-sequential. Values can be of dissimilar data type

<?php
	$a[0]=100;
	$a[10]="abc";
	$a["xyz"]=200;
?>

PHP Null

NULL is a special data type that can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned. If a variable is created without a value, it is automatically assigned a value of NULL.

<?php
	$x = "Hello world!";
	$x = null;
	var_dump($x);
?>

PHP Object

Objects are defined as instances of user-defined classes that can hold both values and functions and information for processing data specific to the class.

PHP Resource

Resources in PHP are not a precise data type. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. it is an external resource.