Hi Every Body

How I can add leading zeros in a number for example
if No. is 4 it should display 0004
if No. is 41 it should display 0041
if No. is 441 it should display 0441

Please give some hint.

5 Years
Discussion Span
Last Post by diafol

how about trying this one
assuming $num is the number you want to fill

$num = "4";
$len = strlen($num);

$zeros = 5-$len;    
$no = "";
    $no .="0";
$no = $no.$num;
echo $no;

Edited by code739


try with this

$number = 41;
printf("%04d\n", $number);

printf() in php, follows some internally implemented rules for formating the numbers based on the given expression in the left side of the statemnt as shown above

please see manual for more details about this method

let me know the status

happy coidng

Edited by radhakrishna.p: clarity on the solution

Votes + Comments
ahahah cool i really didnt think of that thing nice job

to be more specific str_pad() does well for this kind of requirement

$input = "41";
echo str_pad($input, 10,"0", STR_PAD_LEFT);                  

please check this too


I use the str_pad method for this sort of thing, but can I ask why you need this? Is it necessary?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.