PHP data type conversion is a forced conversion. The PHP data types that are allowed to be converted are:
1. (int), (integer): Convert to integer
2. (float), (double), (real): Convert to floating point type
3. (string): Convert to string
4. (bool), (boolean): Convert to Boolean type
5. (array): Convert to array
6. (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
2. Use three specific types of conversion functions, intval(), floatval(), strval()
3. Use the general type conversion function settype(mixed var, string type)
The first conversion method: (int) (bool) (float) (string) (array) (object)
Copy code The code is as follows:
$num1=3.14;
$num2=(int)$num1;
var_dump($num1); //Output float(3.14)
var_dump($num2); //Output int(3)
?>
The second conversion method: intval() floatval() strval()
Copy code The code 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"
?>
The third conversion method: settype();
Copy code The code is as follows:
$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, starting now. Changing the present is changing the future.