PHP is loosely typed, meaning that it will determine the data type at the time data is assigned to each variable. So a variable needs not to be declared before being set. It is declared automatically when you use it.
The eight standard data types available in PHP are as follows:
Type | Description | Example |
---|---|---|
Boolean | One of the special values true or false | true |
Integer | A whole number | 10 |
Float or Double | A floating-point number | 3.14 |
String | A collection of characters | "hello" |
Object | An instance of a class | define a class user $a = new user; |
Array | An ordered set of keys and values | array(); |
Resource | Reference to a third-party resource (a database, for example) | user.db |
NULL | An uninitialized variable |
It's useful to know the type of a variable because sometimes a data is passed from another source and you want to know that the data is of the correct type before using it.
In situations where a specific type of data is required, PHP attempts to convert the data types automatically.