Home  >  Article  >  Backend Development  >  Server Setup Instructions_PHP Tutorial

Server Setup Instructions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:26:34777browse

Variable type change (juggling)
PHP does not need (or does not support) to declare the variable type clearly; the type of a variable is determined by the context in which the variable is used, that is, if you When a string value is assigned 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 with a value of "0" (ASCII 48)
$foo++; // $foo is a string with a value of "1" (ASCII 48) 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 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 casting exists in PHP It's roughly the same in C language: write the required type in parentheses in front of the variable to be forced.
$foo = 10; // $foo is an integer
$bar = (double) $foo; // $bar is a double
The following coercion methods are allowed:
(int), (integer) – coercion to integer
(real), (double), (float) – coercion to double precision number
(string) – coercion to string
(array) – coercion into array
(object) – coerce into object
Note that tabs and spaces are allowed in parentheses, so the following statements are equivalent:
$foo = (int ) $bar;
$foo = ( int ) $bar;

String conversion
When a string is calculated as a numeric value, its result and type are as follows stated decision.
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 the following notation, 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 The string contains the characters e
For more information, please refer to the Unix manual section on strtod(3).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531949.htmlTechArticleVariable type change (juggling) 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 context in which the variable is used...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn