如何在php中格式化数值?

I have to print a numeric value which is less than 10 in the following format:

for 0 I want to print it 00
for 1 I want to print it 01
for 2 I want to print it 02

Are there any php library functions which will format numbers in this way?

Use printf or sprintf:

$i = 1;
printf('%02d', $i);

See it in action.

The sprintf page also documents the syntax for formatting specifiers (%02d), so you can also see what other options are available.

printf('%02u', $number)

http://php.net/sprintf

You can check if the value is less than ten and concatenate using the dot operator '.', i.e.

if($i < 10 && $i >0) print '0'.$i;
else print $i;

Use str_pad function.

str_pad($number_str,2,'0',STR_PAD_LEFT)

PHP Manual - str-pad