Home > Article > Backend Development > PHP Section 2 Data Type Conversion_PHP Tutorial
A variable is considered NULL
when:
is assigned the value NULL
.
has not been assigned a value yet.
is unset().
NULL
type has only one value, which is the case-insensitive keyword NULL
(you can write it as NULL or null).
Converting a variable to the null type will delete the variable and unset its value.
PHP does not require (or support) explicit type definitions in variable definitions; the variable type is determined based on the context in which the variable is used. In other words, if a string value is assigned to the variable var
, var
becomes a string. If you assign an integer value to var
, it becomes an integer.
An example of PHP's automatic type conversion is the plus sign "+". If any operand is a floating point number, all operands are treated as floating point numbers, and the result is also a floating point number. Otherwise the operands are interpreted as integers and the result is also an integer. Note that this does not change the types of the operands themselves; only how the operands are evaluated and the type of the expression itself is changed.
Type casting in PHP is very much like in C: the variable to be converted is preceded by the target type enclosed in parentheses.
(binary) conversion will prefix the result with 'b', new in PHP 5.2.1.