PHP Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
Data types which are supported by PHP are String, Integer, Float, Boolean, Array, Object and NULL. In PHP, it is not necessary to specify data type while declaring a variable.
PHP Data Types | Description | Declaration |
---|---|---|
Boolean | Boolean data types are used in conditional testing and can hold either TRUE or FALSE. | $x = true; if($x){ echo "true";} |
Float | Float variables can hold numbers containing fractional or decimal parts including positive and negative numbers. | $x = 10.5; echo $x; |
Integer | Integers hold only whole numbers including positive and negative numbers. | $x = 100; echo $x; |
String | A string is a non - numeric data type. It can hold alphabets, numbers and special characters.String values must be enclosed within quotes(single or double quotes). | $x = "Hello"; $y = ' World'; echo $x.$y; |
Array | Array is a compound data type that can store multiple values of the same data type. | $fruits = array("Apple", "Banana", "Orange"); echo "I eat " . $fruits[0] . " and " . $fruits[2]; |
Object | Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared using the keyword 'new'. | class abc{ function hello() { echo 5;} } $obj = new abc(); echo $obj->hello(); |
Null | These are special types of variables that can hold only 'NULL'. If a variable is created without a value, it is automatically assigned a value of NULL. | $x = NULL; |
Note: There is another special, not exact, data type named 'Resource' in PHP. A 'resource' is a special variable which holds a reference to an external resource. A resource variable holds special handles to opened files or database connections. For example, fopen() function opens a file and its reference is stored in a resource variable.