PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
- Scalar Types (predefined)
- Compound Types (user-defined)
- Special Types
PHP Data Types: Scalar Types
It holds only single value. There are 4 scalar data types in PHP.
- boolean
- integer
- float
- string
PHP Data Types: Compound Types
It can hold multiple values. There are 2 compound data types in PHP.
- array
- object
PHP Data Types: Special Types
There are 2 special data types in PHP.
- resource
- NULL
PHP Boolean
Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or FALSE(0). Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also considered false in boolean. If a string is empty then it is also considered false in boolean data type.
Ex.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP Boolean</title> </head> <body> <?php if(TRUE) echo "This condition is TRUE"; if(FALSE) echo "This condition is not TRUE"; ?> </body> </html> |
Output:
This condition is TRUE
Note: Only show the TRUE(1) result if all conditional statement is FALSE(0) then not print any result.
PHP Integer
Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They 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 range of integers must lie between -2^31 to 2^31.
Ex.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP Integer</title> </head> <body> <?php $dec1 = 34; $oct1 = 0243; $hexa1 = 0x45; echo "Decimal number: " .$dec1. "</br>"; echo "Octal number: " .$oct1. "</br>"; echo "HexaDecimal number: " .$hexa1. "</br>"; ?> </body> </html> |
Output:
Decimal number: 34
Octal number: 163
HexaDecimal number: 69
PHP Float
A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.
Ex.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP Float</title> </head> <body> <?php $x = 19.34; $y = 54.472; $z = $x + $y; echo "Addition of floating numbers: " .$z; ?> </body> </html> |
Output:
Addition of floating numbers: 73.812
PHP String
Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes, but they will be treated differently while printing variables. To clarify this look at the example below.
Ex.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP String</title> </head> <body> <?php $name = "Dharmendra"; echo "The name of the Geek is $name \n"; echo "</br>"; echo 'The name of the geek is $name'; ?> </body> </html> |
Output:
The name of the Geek is Dharmendra
The name of the geek is $name
PHP Array
Array is a compound data-type that can store multiple values of the same data type. Below is an example of an array of integers. It combines series of data that are related together.
Ex.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP Array</title> </head> <body> <?php $bikes = array ("Royal Enfield", "Yamaha", "KTM"); var_dump($bikes); //the var_dump() function returns the datatype and values echo "</br>"; echo "Array Element1: $bikes[0] </br>"; echo "Array Element2: $bikes[1] </br>"; echo "Array Element3: $bikes[2] </br>"; ?> </body> </html> |
Output:
array(3) { [0]=> string(13) “Royal Enfield” [1]=> string(6) “Yamaha” [2]=> string(3) “KTM” }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM
PHP object
Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.
Ex.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP object</title> </head> <body> <?php class bike { function model() { $model_name = "Royal Enfield"; echo "Bike Model: " .$model_name; } } $obj = new bike(); $obj -> model(); ?> </html> |
Output:
Bike Model: Royal Enfield
PHP Resource
Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource. Resource variables hold special handles to files and database connections.
We will discuss resources in detail in further articles.
PHP Null
These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but it’s case-sensitive. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters.
Note:
- To check the type and value of an expression, use the var_dump() function which dumps information about a variable.
- PHP allows the developer to cast the data type.