Home > Article > Backend Development > How to convert to int type in php?
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
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.int 123
3. Use the general type conversion function settype(mixed var,string type)
Output:
boolean true int 123Related recommendations:
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!