Gene Dopuda
Gene Dopuda

Reputation: 3

PHP: For loop will not execute

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.

Upvotes: 0

Views: 646

Answers (3)

davidvelilla
davidvelilla

Reputation: 488

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.

Upvotes: 2

Priyank
Priyank

Reputation: 3868

Try like this:

for ($i=0; $i<=50; $i++){
        echo 'something';
        unset ($bunnyList[rand(0,(count($bunnyList)))]);
        array_values($bunnyList);
     }

Upvotes: 0

Kevvvvyp
Kevvvvyp

Reputation: 1774

I'm not too familiar with php, more java, but shouldn't it for each time 'I' is less than 50, increment by 1?

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

I am assuming that is the syntax.

Upvotes: 0

Related Questions