PHP-增加一个值

PHP-增加一个值,php,Php,我想增加一个3位数格式的值 例如: $start_value = 000; while () { do something; $start_value++; } 通过这种方式,我得到了以下结果: $start_value = 000; $start_value = 1; $start_value = 2; $start_value = 3; 等等 而不是'001',002',003' 如何实现此结果?使用可以通过以下方式实现: echo sprintf("%03d", $start_val

我想增加一个3位数格式的值

例如:

$start_value = 000;
while () {
do something;
$start_value++;
}
通过这种方式,我得到了以下结果:

$start_value = 000;
$start_value = 1;
$start_value = 2;
$start_value = 3; 
等等

而不是
'001',002',003'

如何实现此结果?

使用可以通过以下方式实现:

echo sprintf("%03d", $start_value++) . "<br>";

你在这里犯了一个大错误
000
中的code是一个八进制数。在许多编程语言中,任何以0开头的文字数都被视为八进制文字。如果要存储000,则需要字符串,而不是数字

$start_value = "000";
while((int) $start_value /*--apply condition --*/) {
    do something;
    $start_value = str_pad((int) $start_value+1, 3 ,"0",STR_PAD_LEFT);
}

事情是这样的。在PHP中没有数据类型的概念,一切都是在运行时根据使用的位置和方式确定的

<?php
 $start_value = 000;
 while ($start_value < 10) {
 //your logic goes here
  $start_value++;
 printf("[%03s]\n",$start_value);
 }
?>

输出:[001][002][003][004][005][006][007][008][009][010]

所以你可以做所有的计算。无论何时要打印值,都可以使用printf和格式说明符。!!希望能有帮助

<?php
 $start_value = 000;
 while ($start_value < 10) {
 //your logic goes here
  $start_value++;
 printf("[%03s]\n",$start_value);
 }
?>