<?php
// initial value
$week = 50;
$year = 2001;
$store = array();
do
{
$week++;
$result = "$week/$year";
array_push($store,$result);
if($week == 53){
$week = 0;
$year++;//increment year by 1
}
continue;
}
// End of Loop
while ($result !== "2/2002");
?>
print_r($store);
Run Code Online (Sandbox Code Playgroud)
结果想要回归
array("51/2001", "52/2001", "01/2002", "02/2002");
Run Code Online (Sandbox Code Playgroud)
使用do..while时继续使用我的问题是什么?
array_push
是错误的.阅读您使用的功能的手册条目.打开服务器上的警告; 在键盘中运行它会立即向我显示问题.$i
而不是$week
.固定代码(现场演示):
<?php
// initial value
$week = 50;
$year = 2001;
$store = array();
do
{
$week++;
$result = "$week/$year";
array_push($store, $result);
if($week == 53){
$week = 0;
$year++;//increment year by 1
}
continue;
}
// End of Loop
while ($result !== "2/2002");
?>
Run Code Online (Sandbox Code Playgroud)
一般来说,我建议像这样反对循环.正如您所发现的,您的代码非常脆弱,因为您只测试了一个非常具体的值,如果该值不正确,则会得到无限循环.
相反,请考虑进行比较$week
,$year
单独和数字:
while ($week < 2 && $year <= 2002)
Run Code Online (Sandbox Code Playgroud)
下次请在您的问题中包含您正在看到的输出,以及您想要查看的输出.这将节省我们重现你的问题的时间.
归档时间: |
|
查看次数: |
1575 次 |
最近记录: |