PHP does not need (or does not support) explicit type definitions in the variable definition, and 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,var, it becomes a string. If you assign an integer value to Var, it becomes an integer.
An example of automatic type conversion in PHP is the plus sign "+". If any operand is a floating-point number, all operands are treated as floating-point numbers, and the result is floating-point numbers. Otherwise the operand is interpreted as an integer and the result is a whole number. Note that this does not change the type of the operands themselves; it only changes how the operands are evaluated and the type of expression itself.
Type cast
The allowed casts are:
• (int), (integer)-Convert to integral type (integer)
• (bool), (Boolean)-Convert to Boolean (Boolean)
• (float), (double), (real)-Convert to float (float)
• (string)-Convert to String
• (binary)-Convert to binary string (string) (PHP 6)
• (array)-Convert to Arrays (array)
• (object)-Convert to Objects (object)
• (unset)-Convert to NULL (PHP 5)
(binary) The conversion will precede the result with the prefix ' B ', and the PHP 5.2.1 new.
Note spaces and tabs are allowed in parentheses
Converts a string (string) literal and a variable to a binary string (string):
Copy Code code as follows:
<?php
$binary = (binary) $string;
$binary = B "binary string";
?>
If you want to change the type of a variable, see Settype ();
settype-set the type of variable
BOOL Settype (mixed $var, string $type)
Sets the type of the variable var.
The possible values for type are:
• "Boolean" (or "bool", from PHP 4.2.0)
• "Integer" (or "int", from PHP 4.2.0)
• "Float" (available only after PHP 4.2.0, "Double" used in older versions is now deactivated)
• "String"
• "Array"
• "Object"
• "Null" (from PHP 4.2.0)
Returns TRUE on success or FALSE on failure.
Intval (), Floatval (), Strval (), these three functions can also be converted
The following main share of PHP data type conversion knowledge.
PHP data type conversions are cast, and PHP data types that allow conversion are:
(int), (integer): Converting to plastic
(float), (double), (real): Converting to floating point type
(string): Converting to String
(bool), (Boolean): Convert to Boolean type
(array): Converting an array of
(object): Converting to Objects
There are three ways to convert PHP data types:
(1) Precede the variable to be converted with a target type enclosed in parentheses, for example:
(int) (BOOL) (float) (string) (array) (object) The following example illustrates:
Copy Code code as follows:
<?php
$num 1=3.14;
$num 2= (int) $num 1; Cast to int type
Var_dump ($num 1); Output float (3.14)
Var_dump ($num 2); Output int (3)
(2) Use 3 specific types of conversion functions, intval (), Floatval (), Strval (), examples are as follows:
Copy Code code as follows:
<?php
$str = "123.9ABC";
$int =intval ($STR); Value after conversion: 123
$float =floatval ($STR); Value after conversion: 123.9
$str =strval ($float); After conversion string: "123.9"
(3) Use the common type conversion function Settype (mixed var,string type), as follows:
Copy Code code as follows:
<?php
$num 4=12.8;
$FLG =settype ($num 4, "int");
Var_dump ($FLG); output bool (TRUE)
Var_dump ($num 4); Output int (12)