duandang2123 2015-03-06 11:44
浏览 62
已采纳

PHP:For循环不会执行

I am at a complete loss. I've searched this site extensively and others as to why my for loop, among many other working loops, will not execute, and tried many suggestions. I have checked that it is in fact not executing and not failing to meet a condition for its execution. This is the loop:

 if (count($bunnyList)>100){
        echo "Too many bunnies! Initiating mass cull.</br>";
        for ($i=0; $i===50; $i++){
            echo 'something';
            unset ($bunnyList[rand(0,(count($bunnyList)))]);
            array_values($bunnyList);
         }
        echo 'Number of bunnies: '.(count($bunnyList));

The if condition executes; the first echo statement executes, and then the echo statement following the loop also executes. If the loop had been executed, then I should get a number of "something"s printed to the page, and yet I never have, even after trying tons of suggestions for other people's failed for loops. After staring at this particular piece of code for hours I'm reasonably sure I haven't messed up a piece of syntax. Please help me, I've already pulled too many chunks of my hair ou :(

The entire code is over a hundred lines long and I didn't think it prudent to post the whole thing. In addition, all the other for loops within the program work just fine, while endless variations of this one do not.

I apologize if this is a redundant question or if it has been asked before. Other answers to questions similar to mine were not able to fix the problem. Please help me D:

TL;DR Why does only this for loop not execute within my program containing many functioning loops? The echo statements within the if condition do execute.

  • 写回答

3条回答 默认 最新

  • duanhong1985 2015-03-06 11:48
    关注

    Try:

    for ($i = 0; $i < 50; $i++) {
    

    Instead of:

    for ($i = 0; $i === 50; $i++) {
    

    In a for loop, the second part is NOT the ending condition, but the continuation condition. In your case it's checking if $i is equals to 50, and because it is not, it will never go in the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形