How to convert php to digital format: 1. Add the target type "(int)", "(float)", etc. enclosed in parentheses before the variable to be converted, and convert it to an integer or Floating point number type; 2. Use the intval() or floatval() function to convert to an integer or floating point number; 3. Use the settype() function.
The operating environment of this tutorial: Windows 7 system, PHP 7.1 version, DELL G3 computer
Convert php to numbers Format
1. Add the target type enclosed in parentheses before the variable to be converted and convert it to an integer or floating point number type
(int)
, (integer)
: Convert to integer type;
(float )
, (double)
, (real)
: Convert to floating point type;
Example:
<?php $str = '123.456abc'; var_dump($str); $int1 = (int)$str; var_dump($int1); $int2 = (integer)$str; var_dump($int2); $float1 = (float)$str; var_dump($float1); $float2 = (double)$str; var_dump($float2); $float3 = (real)$str; var_dump($float3); ?>
Output result:
Method 2: Use conversion function intval(), floatval()
intval(): used to get the integer value of the variable;
floatval(): used to get the floating point value of the variable;
Example:
<?php $str = '123.456abc'; var_dump($str); $int = intval($str); var_dump($int); $float = floatval($str); var_dump($float);
Output result:
##Method 3: Use settype() function
The syntax format of this function is as follows:settype(mixed &$var, string $type)
<?php header("Content-type:text/html;charset=utf-8"); $str = '123.456abc'; var_dump($str); settype($str, 'integer'); var_dump($str); $str = '123.456abc'; settype($str, 'float'); var_dump($str);
The above is the detailed content of How to convert php to digital format. For more information, please follow other related articles on the PHP Chinese website!