Php's data type conversions are cast, and the PHP data types that are allowed to be converted Are:
- (int), (integer): Convert to reshape
- (float), (double), (real): convert to floating-point type
- (string): converted to a string
- (bool), (boolean): converted to Boolean type
- (array): Convert an array
- (object): Convert to Object
There are three ways to convert PHP data types:
- Precede the variable you want to convert with a target type enclosed in parentheses
- Use 3 specific types of conversion functions, intval (), floatval (), strval ()
- Use the common type conversion function Settype (mixed var,string Type)
First conversion mode: (int) (bool) (float) (string) (array) (object)
- <?php
- $num 1=3.14;
- $num 2= (int) $num 1;
- Var_dump ($num 1); Output float (3.14)
- Var_dump ($num 2); Output int (3)
- ?>
Second mode of conversion: intval () floatval () strval ()
- <?php
- $str = "123.9abc";
- $int =intval ($str); Post-conversion Value: 123
- $float =floatval ($str); Post-conversion value: 123.9
- $str =strval ($float); Converted String: "123.9"
- ?>
The third mode of conversion: Settype ();
- <?php
- $num 4=12.8;
- $FLG =settype ($num 4, "int");
- Var_dump ($flg); output BOOL (true)
- Var_dump ($num 4); Output int (12)
- ?>
PHP type Conversion