Conversion method: 1. Add "(int)" before the variable to be converted to force conversion to int type, for example "(int)3.14", the value is 3; 2. Use the intval() function , the syntax is "intval(value)"; 3. Use the settype() function, the syntax is "settype(value,"int")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
There are three PHP data types A conversion method:
1. Forced type conversion
Type coercion in PHP is very similar to that in C: in the The variable is preceded by the target type enclosed in parentheses.
The allowed casts are:
(int),(integer) - Convert to integer type
(bool ),(boolean) - Convert to Boolean type
(float),(double),(real) - Convert to floating point type
(string) through
<?php $num1=3.14; $num2=(int)$num1; var_dump($num1); //输出float(3.14) var_dump($num2); //输出int(3) ?>
float 3.14 int 3
PHP has 3 specific types of conversion functions:
Returns the integer value of variable var by using the specified base conversion (default is decimal). intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.
floatval(): used to obtain the floating point value of a variable.<?php $str="123.9abc"; $int=intval($str); //转换后数值:123 var_dump($int); ?>
int 123
3. Use the general type conversion function settype(mixed var,string type)
<?php $str="123.9sdc"; $int=settype($str,"int"); var_dump($int); //输出bool(true) var_dump($str); //输出int(12) ?>
Output:
boolean true int 123
php training
The above is the detailed content of How to convert to int type in php?. For more information, please follow other related articles on the PHP Chinese website!