Home  >  Article  >  Backend Development  >  How to get the digit value of a number in php

How to get the digit value of a number in php

青灯夜游
青灯夜游Original
2022-03-10 19:25:402248browse

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.

How to get the digit value of a number in php

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>";
?>

How to get the digit value of a number in php

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>";
?>

How to get the digit value of a number in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn