php字符串增加1怎么实现
php字符串增加1的实现方法:1、新建php示例文件;2、创建“function inc($s) {…}”方法;3、通过“while ($n– > 0) {$s = inc($s);}”计算除去最后一个字符的字符串,然后进位即可。

本教程操作环境:Windows10系统、PHP8.1版、DELL G3电脑
php字符串增加1怎么实现?
php 数字字符串 +1 操作 累加 加一 (不溢出)
* inc.php
<?php
function inc($s) {
if (empty($s)) {return "1";}
if ( ! isset($s[1]) ) {
$code = ord($s[0]) + 1;
if ($code <= ord("9")) {
return chr($code);
}
return "10";
}
$n = strlen($s);
$code = ord($s[$n-1]) +1;
if ($code <= ord("9")) {
return substr($s, 0, $n-1).chr($code);
}
return inc(substr($s, 0, $n-1))."0";
}
$s = "2147483647";
$n = 10000;
while ($n-- > 0) {
// printf("%s
", $s);
$s = inc($s);
}
printf("%s
", $s); // "2147493647"
登录后复制

