Home >Backend Development >PHP Tutorial >Introduction to the method of setting PHP server variables_PHP tutorial
Variable type juggling
PHP does not require (or does not support) specifying its variable type in the declared variable; the type of a variable is determined by this variable Determined by the context of use, that is, if you assign a string value to a variable var, var becomes a string variable. If you assign an integer to var, it becomes an integer variable.
An example of PHP automatically converting variable types is the addition operator '+'. If any operand is a double, then all operands are evaluated as doubles, and the result is also a double. Otherwise, the operands will be considered to be integers and the result will be an integer. Note that this does not affect the variable type of each operand itself, the only change is how the operands are processed during the calculation.
$foo = "0"; // $foo is a string, the value is "0" (ASCII 48)
$foo++; / / $foo is a string with value "1" (ASCII 49)
$foo += 1; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a double (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is an integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is an integer (15)
If you think the last two expressions in the above example look a bit strange, please see the "String Conversion" section.
If you wish to force a variable to be evaluated as a fixed type, see the "Casting" section. If you wish to change the type of a variable, please see the description of the function "settype()".
Determining the type of a variable
Because PHP determines the type of variables itself and generally converts them as needed, the type of a particular variable is not immediately obvious . PHP includes functions to find out the type of this variable. These functions are gettype(), is_long(), is_double(), is_string(), is_array(), and is_object().
Type casting
Type coercion in PHP is roughly the same as in C language: write the required type in parentheses in front of the variable to be coerced.
$foo = 10; // $foo is an integer
$bar = (double) $foo; // $bar is a double The following coercion methods for numbers
are allowed:
(int), (integer) - coercion to integers
(real), (double), ( float) – coerced to a double
(string) – coerced to a string
(array) – coerced to an array
(object) – coerced to an object
Note that tabs and spaces are allowed within parentheses, so the following statements are equivalent:
$foo = (int) $bar;
$foo = ( int ) $bar;
String conversion
When a string is treated as When calculating a numeric value, its result and type are determined as described below.
If this string contains the characters '.', 'e', or 'E', it is treated as a double type variable, otherwise it is treated as an integer.
The value of this string is determined by the prefix. If the string begins with any valid numeric data, then the numeric data is the value on which the string is evaluated. Otherwise, the value is zero. Valid numeric data follows these notations, followed by one or more digits (which may include a decimal point), followed by an optional exponent. The exponent is formed by one or more digits followed by 'e' or 'E'.
$foo = 1 + "10.5"; // $foo is a double (11.5)
$foo = 1 + "-1.3e3 "; // $foo is a double precision number (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is an integer (1)
$foo = 1 + "bob3"; // $foo is an integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is an integer (11)
$foo = 1 + "10 Little Piggies"; // $foo is an integer (11);
// This string includes the characters 'e'