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.
echo gettype($x); # integer
settype($x,"string"); # sets type to string
echo gettype($x); # 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
$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
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.
echo "This condition is TRUE.";
echo "This condition is FALSE.";
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).
// decimal base integers
$deci1 = 50;
$deci2 = 654;
// octal base integers
$octal1 = 07;
// hexadecimal base integers
$octal = 0x45;
$sum = $deci1 + $deci2;
A float (floating point number) is a number with one decimal point or a number in exponential form.
$x = 10.365;
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
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.
$x = "Hello world!";
$x = null;
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.
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.