零填充一个整数 php

2022-08-30 13:48:08

如何“零填充”整数?

1 becomes 0001
40 becomes 0040
174 becomes 0174

答案 1
$filled_int = sprintf("%04d", $your_int)

答案 2
$number = 12;
$width = 4;
$padded = str_pad((string)$number, $width, "0", STR_PAD_LEFT); 

推荐