Variable type change (juggling) PHP does not need (or is not supported) to define its variable type in the famous variable; the type of a variable is determined by the relationship between the variables used, that is to say, if you assign a string value to a variable var, var becomes a string variable. If you change SyntaxHighlighter. all ();
Variable type change (juggling)
PHP does not need (or does not support) to specify the variable type in the name variable; the type of a variable is determined by the relationship between the variables used, 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's automatic conversion of variable types is the addition operator '+ '. If any operand is a double-precision number, all operands are evaluated as double-precision numbers, and the result is also a double-precision number. Otherwise, the operand is considered an integer, and the result is also an integer. Note that this does not affect the variable type of each operand. the only change is how the operand is processed during calculation.
$ Foo = "0"; // $ foo is a string with a value of "0" (ASCII 48)
$ Foo ++; // $ foo is a string with the value "1" (ASCII 49)
$ Foo + = 1; // $ foo is now an integer (2)
$ Foo = $ foo + 1.3; // $ foo is a number of double precision (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 seem a bit strange, please refer to the "string conversion" section.
If you want to force a variable to be calculated as a fixed type, see the "casting" section. If you want to change the type of a variable, see the description of the function "settype.
Determine the type of a variable
Because PHP decides the type of variables and converts them as needed, the type of a specific variable is not very obvious at any time. PHP includes some 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)
In PHP, the type is forced to be roughly the same as in C: write the required type in parentheses before the variable to be forced.
$ Foo = 10; // $ foo is an integer.
$ Bar = (double) $ foo; // $ bar is a double number.
The following mandatory methods are allowed:
(Int), (integer )? Forced integer
(Real), (double), (float )? Forced double precision count
(String )? Forced string
(Array )? Forced array
(Object )? Forced object
Note that the parentheses allow both tabs (tabs) and spaces (spaces), so the following statements are equivalent:
$ Foo = (int) $ bar;
$ Foo = (int) $ bar;
String conversion
When a string is calculated as a numeric value, its results and types are determined as described below.
If the string contains characters '.', 'e', or 'e', it is treated as a double-precision variable. Otherwise, it is treated as an integer.
The value of this string is determined by the first part of the word. If the string starts with any valid numeric data, the numeric data is the value of this string. Otherwise, the value is zero ). Valid numeric data follows the tags below, following one or more numbers (which can contain decimal points) and following an optional index. An index consists of one or more numbers following 'E' or 'e.
$ Foo = 1 + "10.5"; // $ foo is a double precision (11.5)
$ Foo = 1 + "-1.3e3"; // $ foo is a double precision (-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 contains the character e
For more information, please refer to the section about strtodd (3) in the Unix manual.