Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php format a number so that it has leading zero [duplicate]

Tags:

php

I want to format my array vars to that numbers under ten have leading zeros, so:

$myArray=array(1,2,3,10,11,100);

So that it is:

$myArray=array(01,02,03,10,11,100);

Is there an easy function for this?

Thanks in advance.

like image 238
user783322 Avatar asked Mar 09 '26 13:03

user783322


1 Answers

In php, a leading zero means the number is a octal number.

You should just format it at the output time like:

$myArray=array(1,2,3,10,11,100);
foreach ($myArray as $var) {
  echo sprintf("%02d\n", $var);
}
like image 167
xdazz Avatar answered Mar 12 '26 02:03

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!