PHP supports the following eight data types:
- boolean
- integer
- float
- string
- array
- object
- resource
- null
Boolean
PHP boolean data type is anyone of true or false. Both true and false data types are case insensitive. You can learn more about this in PHP control structure.
1 |
Syntax: true, false |
Example
1 2 3 4 5 6 |
<?php $a = 5; // $a has a true value $x = true; // explicitly define true value $y = ""; // $y has a false value $z = false; // explicitly define false value ?> |
Integer
An integer is a number and should not contain any alphabet, comma, or blanks. The range of integer can be -2,147,483,648 to + 2,147,483,647. Integer can be specified in the following three formats: decimal (10 base), hexadecimal (16 base), binary (2 base). It may be positive or negative within the range.
Example
1 2 3 4 5 |
<?php $x = 5; $y = +5; $z = -5; ?> |
Floating-Point Numbers
PHP float is a number which should contain at least one decimal point or represented by exponential form.
Example
1 2 3 4 5 |
<?php $x = 5.07; $y = +5.07; $z = -5.07; ?> |
Strings
PHP string is any character represented within single (‘) or double (“) quotes.
Example
1 2 3 4 |
<?php $message = 'Hello user'; $message = "Hello user"; ?> |
If a string is defined within single quote and you want same single quote to be specified within the text, then you should escape it with backslash (\)
Arrays
PHP array is a compound data type which contains one or more values and each value associated with the key.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $planets[0] = 'sun'; $planets[1] = 'moon'; $employers['age'] = 30; $employers['name'] = 'King'; //We can re-write above variables like below $planets = array('sun', 'moon'); $employers = array( 'age' => 30, 'name' => 'king' ); ?> |
Objects
PHP object is a compound data type which contain both data and methods. To create a object you have to instantiate a class using “new” keyword.
Example
1 2 3 4 5 6 |
<?php class car { function moving(){} } $object_1 = new car(); // object created ?> |
Resources
PHP resource is a special variable. It is the reference of an external resources. In the database connectivity and handling of file processing, the resource variable exists.
NULL
PHP null is a special data type which represent a variable having empty or no value. It is case insensitive.
1 |
Syntax: NULL |
If you have unset the variable then it will be considered as a variable with null value.