Three methods of data type conversion in PHP and three methods of data type conversion. Three methods of data type conversion in PHP, three methods of data type conversion in PHP are forced conversion. The PHP Data types that can be converted include: 1. (int), (integer): three methods for converting data types to PHP, three methods for converting data types
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.
PHP Data type conversion is a forced conversion. The PHP Data types that can be converted include: 1. (int) and (integer...