Home > Article > Backend Development > How to set PHP server variables_PHP tutorial
PHP does not need (or does not support) to specify its variable type in the declared variable; the type of a variable is determined by the time before and after the variable is used Determined by relationship, 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, the value is "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()".
Determine the type of a variable
Because PHP determines the types of variables itself and generally casts them as needed, the type of a particular variable is not always 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 precision number
The following coercion methods are allowed:
(int), (integer) – forced to integer
(real), (double), (float) – forced to double precision number
(string) – forced to string
(array) – forced to array
(object) – forced to 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 evaluated as a numeric value, its result and type are determined as described below.
If this string contains the characters '.', 'e', or 'E', it will be treated as a double type variable, otherwise it will be 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 precision number (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 character 'e'
For more information, please refer to the Unix manual section on strtod(3).