在php中从0000开始递增数字 [英] Incrementing numbers starting from 0000 in php

查看:541
本文介绍了在php中从0000开始递增数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关php计数器功能的建议.是否有任何函数将具有5位数字的数字表示为00001或00123…此数字应不是随机的,而必须增加前一个字段的值.

I need a suggestion on a function for a php counter. Is there any function to have numbers with 5 digit as 00001, or 00123… this number should be not random but have to increase the value of a previous field.

如果数字是$ n ='00001',有一个函数加1并得到00002而不是2?

If a number is $n=’00001’ there is a function to increase by one and get 00002 and not 2?

谢谢 F.

推荐答案

$n2 = str_pad($n + 1, 5, 0, STR_PAD_LEFT);

通过添加0 s使用 str_pad() (<在旧数字$n的左侧(第四参数)中添加strong>第三参数),并增加1(第一参数),直到长度为5(参数).

Use str_pad() by adding 0s (third parameter) to the left (fourth parameter) of the old number $n incremented by 1 (first parameter) until the length is 5 (second parameter).

这篇关于在php中从0000开始递增数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆