PHP is a weak type language, because we do not need to specify the data type it stores when declaring a variable. However, although PHP is a weak language, sometimes it still needs to use type conversion. PHP is a weak type language, because we do not need to specify the data type it stores when declaring a variable. However, although PHP is a weak language, sometimes it still needs to use type conversion.
The following table lists the conversion types allowed by PHP:
Conversion operator |
Conversion type |
Example |
Boolean, bool |
Convert to Boolean type |
(Boolean) $ num, (boolean) $ str |
String |
Convert to string |
(String) $ boo, (string) $ flo |
Integer, int |
Convert to integer |
(Integer) $ boo, (integer) $ str |
Float, double |
Convert to floating point |
(Float) $ str |
Array |
Convert to array |
(Array) $ str |
Object |
Convert to object |
(Object) $ str |
Type conversion in PHP is very simple,There are three conversion methods:
First:
You only need to add the type name enclosed in parentheses before the variable to be converted, as shown below:
";var_dump($num2);?>
Code running result:
Second:
Use three types of conversion functions: intval (), floatval (), strval ()
"; $ Float = floatval ($ a); // converted value: 123.9var _ dump ($ float); echo"
"; $ Str = strval ($ float); // The converted string:" 123.9 "var_dump ($ str);?>
Code running result:
Third:
Using the settype () function, this function can convert a specified variable to a specified data type. The syntax is as follows:
settype(mixed var,string type)
The var parameter is the specified variable. the type parameter is the specified data type. The type parameter has seven optional values: boolean, float, integer, array, null, object, and string. if the conversion is successful, the setype () function returns true; otherwise, the system returns false.
"; Var_dump ($ num); // output int (12)?>
Code running result:
When a string is converted to an integer or floating point type, if the string starts with a number, it first converts the numeric part to an integer type and removes the subsequent string. if the number contains a decimal point, the first digit of the decimal number is obtained.
PHP Data type conversion instance
In this example, the first and third methods are used to convert the specified string type and compare the differences between the two methods. the code is as follows:
'; Echo' value of the output variable $ num: '. $ num .'
'; // Output the original variable $ numecho'. use the settype function to convert the variable $ num type: '; echo settype ($ num, 'integer ').'
'; // Use the settype function to convert the echo type. The value of the output variable $ num:'. $ num; // output the original variable $ num?>
Code running result:
From the example above, we can see that the converted variable type can be directly output using the integer operator, and the original variable does not change. Instead, the settype () function returns 1, that is, true, and the original variable is changed. in actual application, you can select the conversion method as needed.
In the next section, we will explain how to check data types ".
The above is a detailed description of the php Data type conversion instance. For more information, see other related articles in the first PHP community!