Home >Backend Development >PHP Problem >How to detect how many digits an integer has in php
php method to detect how many digits an integer has: 1. Use the strval() function to convert the integer into a numeric string, the syntax is "$str = strval($int);"; 2. Use strlen( ) function to get the length of a numeric string (the number of numeric characters), the syntax is "strlen($str)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The number of digits in a natural number is called digits . The number of digits in a natural number is called digits. A number containing one digit is a one-digit number, a number containing two digits is a two-digit number, a number containing three digits is a three-digit number... A number containing n digits is an n-digit number.
For example, 123 occupies three digits, and the number of digits is 3
SoHow to use PHP to detect how many digits an integer has?
Implementation idea:
Convert integers to numeric strings. (Conversion method strval() or settype() function)
Use strlen() or mb_strlen() function to get the length of the numeric string (number of numeric characters)
Implementation code:
<?php header('content-type:text/html;charset=utf-8'); $int= 123; $str = strval($int); $result=strlen($str); echo "整数 ".$int." 是 ".$result." 位数。"; ?>
## Description:
in PHP There are two functions commonly used to calculate the length of strings, namely the strlen() and mb_strlen() functions. When processing all-English strings, the effect of these two functions is the same, but when processing mixed Chinese and English strings or pure Chinese strings, these two functions will have some differences.PHP Video Tutorial"
The above is the detailed content of How to detect how many digits an integer has in php. For more information, please follow other related articles on the PHP Chinese website!