PHP data types defines the type of data a variable can store. PHP allows eight different data types that can be categorized further in 3 types:
There are four scalar data types in PHP.
Integer Data type:
Integers are whole numbers including positive and negative numbers without fractional part or decimal point. Integers can be decimal (base 10), octal (base 8) or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The maximum value of an integer is platform-dependent.
Float/double Data type:
Floating-point numbers are also known as double or real numbers. They can hold numbers containing fractional or decimal part. The maximum value of a float is platform-dependent. Floating-point numbers are larger than integers.
String Data type:
Strings are letters or any character, even numbers also included. These strings are written within a double quote during declaration. You can written strings within a single but it will be treated differently while printing variables as shown in below example:
Boolean Data type:
Boolean is the simplest data type that holds only two values i.e. true and false. The successful events will return true and unsuccessful events return false.
Note: The NULL values are also treated as false in Boolean. Apart from NULL, 0 is also considered as false in boolean. The empty string is also considered as false in boolean data type.
There are two compound data types in PHP.
Array Data type:
An array is a variable that can store multiple values at a time. The below example show the use of array:
Object Data type:
An object is a data type that stores not only data but also information on how to process that data. Objects are defined as instances of user-defined classes that can hold both values and functions.
Special Data types:
There are two special data types in PHP.
Null Data Type:
NULL is the special data type that used to represent empty variables. If a variable is null, it means the variable does not contain any data or value. NULL is the only possible value of type null, it is case sensitive.
Resource Data Type:
Resources are special data types that are used to store references to some function call or external PHP resources. A example of using the resource data type is a database call. for eg:
The function returns a resource type data stored in a $con variable.