Conversion method: 1. Use intval() function, syntax "intval($str)"; 2. Use floatval() function, syntax "floatval($str)"; 3. Use settype() function , syntax "settype($str,"float")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php Lieutenant General Number Convert string to numeric type
#Method 1: Use intval() function to convert to integer type
intval(): used to obtain the variable Integer value;
<?php header("Content-type:text/html;charset=utf-8"); $str = '2334.976'; var_dump(intval($str)); ?>
Method 2: Use floatval() function to convert to floating point type
floatval(): used Get the floating point value of the variable;
<?php header("Content-type:text/html;charset=utf-8"); $str = '2334.976'; var_dump(floatval($str)); ?>
Method 3: Use the settype() function to convert to an integer or floating point type
settype ($var,$type)
The function is used to set the variable $var
to the specified data type $type
.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $str = '2334.976'; settype($str,"integer"); var_dump($str); $str = '2334.976'; settype($str,"float"); var_dump($str); ?>
The value of $type can be:
"boolean" ( or "bool", starting from PHP 4.2.0)
"integer" (or "int", starting from PHP 4.2.0)
"float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)
"string"
"array"
"object"
"null" (from PHP 4.2.0)
# The settype() function will change the original variable.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert numeric string to numeric type in php. For more information, please follow other related articles on the PHP Chinese website!