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

How to get the digit value of a number in php

青灯夜游
Release: 2023-03-15 15:34:02
Original
2340 people have browsed it

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>";
?>
Copy after login

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>";
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template