Home > Article > Backend Development > How to get the digit value of a number in php
Acquisition method: 1. Use the substr() function, the syntax is "substr($num, n-1,1)", the parameter n is the number of digits to obtain the value; 2. Use the mb_substr() function , the syntax is "mb_substr($num, n-1,1)", the parameter n is the number of digits to obtain the value.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php gets the number The first digit value
Method 1: Use the substr() function
<?php header("Content-type:text/html;charset=utf-8"); $num=12345; echo "数字为:".$num."<br>"; echo "数字第1位的值:".substr($num, 0 ,1)."<br>"; echo "数字第2位的值:".substr($num, 1 ,1)."<br>"; echo "数字第3位的值:".substr($num, 2 ,1)."<br>"; ?>
Description: substr( ) function can intercept characters of a certain length from a specified position in a string.
Method 2: Use the mb_substr() function
<?php header("Content-type:text/html;charset=utf-8"); $num=123456; echo "数字为:".$num."<br>"; echo "数字第1位的值:".mb_substr($num, 0 ,1)."<br>"; echo "数字第2位的值:".mb_substr($num, 1 ,1)."<br>"; echo "数字第3位的值:".mb_substr($num, 2 ,1)."<br>"; echo "数字第4位的值:".mb_substr($num, 3 ,1)."<br>"; echo "数字第5位的值:".mb_substr($num, 4 ,1)."<br>"; ?>
Description: The mb_substr() function is similar to the substr() function. Characters of a certain length can be intercepted from the specified position of the string; but unlike the substr() function, the mb_substr() function is not only valid for English characters, but also for Chinese characters.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get the digit value of a number in php. For more information, please follow other related articles on the PHP Chinese website!