PHP does not need (or does not support) explicit type definitions in variable definitions; The variable type is determined by the context in which the variable is used. In other words, if you assign a string value to a variable var, varis a string. If you assign an integer value to the var, then it becomes an integer.
An example of automatic type conversion for PHP is the plus sign "+". If any one operand is a floating-point number, all operands are treated as floating-point numbers, and the result is a floating-point number. Otherwise the operand is interpreted as an integer, and the result is an integer. Note that this does not change the type of the operands themselves; it only changes how the operands are evaluated and the type of the expression itself.
Type casting
The allowable casts are:
- (int), (integer)-Convert to Integer (integer)
- (bool), (Boolean)-Converts to Boolean (Boolean)
- (float), (double), (real)-Convert to floating-point (float)
- (string)-Convert to string (string)
- (binary)-converted to binary string ( string) (PHP 6)
- (array)-converts to arrays (array)
- (object)-Convert to object (objects)
- (unset)-Convert to NULL (PHP 5)
The (binary) conversion will precede the result with the prefix ' B ', PHP 5.2.1 new.
Note that spaces and tabs are allowed inside the parentheses
Converts string literals and variables to binary strings (string):
< span="">= (binary < span=""> < span=""> )< span=""> = B "binary string"< span="">?>
If you want to change the type of a variable, see Settype ();
Settype - Set the type of the variable
BOOL
Settype( mixed $var , string $type )
Set the type of the variable var to type.
The possible values for type are:
- "Boolean" (or "bool", starting from PHP 4.2.0)
- "Integer" (or "int", starting from PHP 4.2.0)
- "Float" (only available after PHP 4.2.0, for "double" used in older versions is now disabled)
- "String"
- "Array"
- "Object"
- "Null" (from PHP 4.2.0)
Returns TRUEon success, or FALSEon failure.
Intval (), Floatval (), Strval (), these three functions can also be converted
http://www.bkjia.com/PHPjc/675205.html www.bkjia.com true http://www.bkjia.com/PHPjc/675205.html techarticle PHP does not need (or does not support) explicit type definitions in variable definitions; The variable type is determined by the context in which the variable is used. In other words, if you assign a string value to ...