Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing numbers starting from 0000 in php

Tags:

php

numbers

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.

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

Thanks F.

like image 220
user1103633 Avatar asked May 26 '12 20:05

user1103633


2 Answers

$number = 1;
$number++;
echo str_pad($number, 5, "0", STR_PAD_LEFT);  //00002
like image 78
John Conde Avatar answered Sep 28 '22 14:09

John Conde


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

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).

like image 44
Jeroen Avatar answered Sep 28 '22 14:09

Jeroen