This article mainly shares the knowledge of PHP Data type conversion.
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 the target type enclosed in parentheses before the variable to be converted, for example:
(Int) (bool) (float) (string) (array) (object) is described in the following example:
<? Php $ num1 = 3.14; $ num2 = (int) $ num1; // force convert 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:
<? Php $ str = "123.9abc"; $ int = intval ($ str); // value after conversion: 123 $ float = floatval ($ str); // value after conversion: 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:
<? Php $ num4 = 12.8; $ flg = settype ($ num4, "int"); var_dump ($ flg); // output bool (true) var_dump ($ num4 ); // output int (12)
Articles you may be interested in
- In php, we use the curl post method to submit data and the get method to obtain webpage data.
- How does php convert the br line breaks in html to line breaks in text input?
- Php converts a multi-dimensional array to a one-dimensional array
- Methods for resetting the auto-increment start value of various MySQL Data Table types
- Php mysql database operations
- Powerful PHP image processing class (watermark, transparency, zoom, sharpen, rotate, flip, cut, reversed)
- Php uses array_flip to implement array key-value exchange to remove array duplicate values
- Introduction to array union, intersection, and difference set functions in php