开发者

how to control interval of for loop?

开发者 https://www.devze.com 2023-01-17 15:08 出处:网络
I have a for loop, with a $count of 23. for($page=0; $page < $count; $page++) { echo $page; } What I want to do is instead of increasing the page variable by one, I want to do it by 10 开发者_运

I have a for loop, with a $count of 23.

for($page=0; $page < $count; $page++) { 
echo $page;
}

What I want to do is instead of increasing the page variable by one, I want to do it by 10 开发者_运维百科so that when $page is echoed it I want the results to be 10, 20, and not go past 23.

for($page=0; $page < $count; $page+10) { 
echo $page;
}

When I do this, the loop continues infinitely, echoing 0. IF anyone has any advice, I would really appreciate it. Thanks in advance.


for($page=0; $page < $count; $page+=10) { 
  echo $page;
}

Just got your increment syntax a bit wrong (should have been page += not page + which doesn't actually update the value of the $page var)

Alternatively you can use the long form/more complex mathematics:

for($page=0; $page < $count; $page=$page+10) { 
  echo $page;
}

word of caution make sure you stick with a ranged condition (< or <=) if you're switching your step size like this, otherwise you can jump past your condition (if it read !=23 for example) and create a never ending loop. Not a problem for you the way you wrote it, but just something to consider.


you can do this

<?PHP 
$count = 23;
for($page=0; $page < $count; $page++) { 
if ($page % 10 == 0 && $page != 0)
    echo $page."</br>";
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消