This article mainly shares the knowledge of PHP Data type conversion. PHP Data type conversion is forced conversion. The PHP Data types that can be converted include: (int), (integer): converted to integer & nbsp; (float), and (double), (real): convert to float type & nbsp; (string): convert to string & nbsp; (bool), (boolean): convert to boolean type & nbsp; (array ): convert to array & nbsp; (object): This article describes how to convert PHP Data types.
PHP Data type conversion is a forced conversion. The PHP Data types that can be converted include:
(Int), (integer): convert to integer
(Float), (double), (real): convert to floating point type
(String): convert to a string
(Bool), (boolean): convert to boolean type
(Array): convert to an array
(Object): convert to object
There are three conversion methods for PHP Data types:
(1) add brackets before the variable to be converted.
Target type, for example: (int) (bool) (float) (string) (array) (object)
The following is an example:
$ Num1 = 3.14;
$ Num2 = (int) $ num1; // forcibly converted to int type
Var_dump ($ num1); // output float (3.14)
Var_dump ($ num2); // output int (3)
(2) use three types of conversion functions: intval (), floatval (), and strval (). The example is as follows:
$ Str = "123.9abc ";
$ Int = intval ($ str); // converted value: 123
$ Float = floatval ($ str); // converted value: 123.9
$ Str = strval ($ float); // converted string: "123.9"
(3) use the common type conversion function settype (mixed var, string type). The example is as follows:
$ Num4 = 12.8;
$ Flg = settype ($ num4, "int ");
Var_dump ($ flg); // output bool (true)
Var_dump ($ num4); // output int (12)