Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment a value with leading zeroes using php

Tags:

php

Increment a value with leading zeroes using php and those numbers must be from counts in the database. For example 0001 to 1000.

like image 293
Theophilus Abiola Alamu Avatar asked Feb 14 '17 08:02

Theophilus Abiola Alamu


People also ask

How can I increment a number by 1 in PHP?

C style increment and decrement operators represented by ++ and -- respectively are defined in PHP also. As the name suggests, ++ the increment operator increments value of operand variable by 1. The Decrement operator -- decrements the value by 1. Both are unary operators as they need only one operand.


1 Answers

use str_pad()

$val = 1;
echo str_pad($val,4,"0",STR_PAD_LEFT); // 0001

$val = 10;
echo str_pad($val,4,"0",STR_PAD_LEFT); // 0010
like image 116
B. Desai Avatar answered Oct 09 '22 13:10

B. Desai