Variables can store data of various types, and different data types serve different purposes.
PHP supports the following data types:
You can determine the data type of any object by using the var_dump()
function.
The var_dump() function provides both the data type and the value.
$x |
NULL is a special data type that can hold only one value: NULL.
A variable of data type NULL is one that has no value assigned to it.
Tip: If a variable is created without an assigned value, it is automatically given a value of NULL.
Variables can also be cleared by setting their value to NULL.
$x $x var_dump($x); |
If you assign an integer value to a variable, its type will automatically be set to integer.
If you then assign a string to the same variable, its type will change to string.
$x
|
If you want to change the data type of an existing variable without altering its value, you can use casting.
Casting enables you to modify the data type of variables.
$x $x var_dump($x); |
The special resource type is not a true data type; it refers to a reference to functions and resources outside of PHP.
A typical example of the resource data type is a database call.