<?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);
result want return will be
array("51/2001", "52/2001", "01/2002", "02/2002");
What is my problems by using while using do..while ,continue?
Your arguments to[Edit: You have now quietly fixed that in your question.]array_pushare the wrong way around. Read the manual entry for functions you use. Turn on warnings on your server; running this in codepad showed me the problem immediately.$iinstead of$week.Fixed code (live demo):
In general, I’d recommend against loops like this. As you’ve discovered, your code is very fragile because you’re testing for just one very specific value, and if that value is not precisely correct you get an infinite loop.
Instead, consider comparing
$weekand$yearseparately and numerically:Next time please include in your question the output that you are seeing, as well as the output that you want to see. It’ll save us time in reproducing your problem.