This article mainly introduces three methods of data type conversion in PHP. This article describes the three conversion methods of PHP Data type conversion and PHP Data type conversion. For more information, see
This article mainly introduces three methods of data type conversion in PHP. This article describes the three conversion methods of PHP Data type conversion and PHP Data type conversion. For more information, see
PHP Data type conversion is a forced conversion. The PHP data types that can be converted include:
1. (int), (integer): Convert to integer
2. (float), (double), (real): Convert to floating point type
3. (string): convert to a string
4. (bool), (boolean): Convert to boolean type
5. (array): convert to an array
6. (object): convert to an object
There are three conversion methods for PHP Data Types:
1. Add the target type enclosed in parentheses before the variable to be converted
2. Use three types of conversion functions: intval (), floatval (), strval ()
3. Use the common type conversion function settype (mixed var, string type)
First conversion method: (int) (bool) (float) (string) (array) (object)
The Code is as follows:
<? Php
$ Num1 = 3.14;
$ Num2 = (int) $ num1;
Var_dump ($ num1); // output float (3.14)
Var_dump ($ num2); // output int (3)
?>
Second Conversion Method: intval () floatval () strval ()
The Code is as follows:
<? Php
$ Str = "123.9abc ";
$ Int = intval ($ str); // converted value: 123
$ Float = floatval ($ str); // converted value: 123.9
$ Str = strval ($ float); // converted string: "123.9"
?>
Third conversion method: settype ();
The Code is as follows:
<? Php
$ Num4 = 12.8;
$ Flg = settype ($ num4, "int ");
Var_dump ($ flg); // output bool (true)
Var_dump ($ num4); // output int (12)
?>
A journey of a thousand miles begins with a single step. Change the future from now on. Changing the present is changing the future.
,