This article mainly shares the knowledge of PHP data type conversion.
PHP data type conversions are cast, and PHP data types that allow conversion are:
(int), (integer): Converting to plastic
(float), (double), (real): Converting to floating point type
(string): Converting to String
(bool), (Boolean): Convert to Boolean type
(array): Converting an array of
(object): Converting to Objects
There are three ways to convert PHP data types:
(1) Precede the variable to be converted with a target type enclosed in parentheses, for example:
(int) (BOOL) (float) (string) (array) (object) The following example illustrates:
<?php
$num 1=3.14;
$num 2= (int) $num 1; Cast to int type
var_dump ($num 1);//output float (3.14)
Var_dump ($num 2);//output int (3)
(2) Use 3 specific types of conversion functions, intval (), Floatval (), Strval (), examples are as follows:
<?php
$str = "123.9ABC";
$int =intval ($STR); Value after conversion: 123
$float =floatval ($STR);//Convert value: 123.9
$str =strval ($float);//Convert after string: "123.9"
(3) Use the common type conversion function Settype (mixed var,string type), as follows:
<?php
$num 4=12.8;
$FLG =settype ($num 4, "int");
Var_dump ($FLG); output bool (TRUE)
Var_dump ($num 4);//output int (12)
Articles that you may be interested in
- Summary of methods for using Curl post submission data and get access to Web page data in PHP
- MySQL various data table types reset the starting value of the method
- How PHP converts a multidimensional array to a one-dimensional array
- How PHP converts a BR line break in HTML into a newline character in text input
- PHP to determine the most secure upload file type, the most realistic solution
- PHP MySQL database operation class
- Powerful PHP Image processing class (watermark, transparency, zoom, sharpen, rotate, flip, cut, invert color)
- PHP uses Array_flip to implement array key exchange to remove duplicate value of array