Section 2. Conversion of data types

  • 2020-05-16 06:31:38
  • OfStack

1 variable is considered to be NULL in the following cases:

Is assigned to NULL.

Has not been assigned.

Be unset ().

The NULL type has only one value, which is the case-insensitive keyword NULL (you can write NULL or null).

Converting a variable to type null will delete the variable and unset its value.

Type conversion

PHP does not require (or support) explicit type definitions in variable definitions; The type of a variable is determined based on the context in which the variable is used. In other words, if a string value is assigned to a variable , var, and , var, it becomes a string. If you assign another integer value to , var, it becomes an integer.

An example of PHP's automatic type conversion is the plus sign "+". If any one operand is a floating point number, all operands are treated as floating point and the result is a floating point number. Otherwise the operand will be interpreted as an integer and the result will be an integer. Note that this does not change the type of the operands themselves; All that has changed is how the operands are evaluated and the type of the expression itself.

Type casts in PHP are very similar to those in C: the target type is bracketed before the variable to be converted.

(int), (integer) - convert to integer (integer) (bool), (boolean) - convert to Boolean (boolean) (float), (double), (real) - convert to floating point (float) (string) - convert to string (string) (binary) - convert to base 2 string (string) (PHP 6) (array) - convert to array (array) (object) - convert to object (object) (unset) - convert to NULL (PHP 5)

The (binary) conversion prefixes the result with 'b' and PHP 5.2.1.


Related articles: