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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog