一般使用如下:
$num = 1; echo str_pad($num, 4, "0", STR_PAD_LEFT); //==> 0001
這函式其實主要是針對字串限定字數若不到字數要加字串在前或後,如下:
$str = 'Alvin'; echo str_pad($str, 10); //==> 'Alvin ' $str = 'Alvin'; echo str_pad($str, 10, "_", STR_PAD_BOTH) //==> '__Alvin___'
參考網址:
Formatting a number with leading zeros in PHP
str_pad