BitByBit
BitByBit

Reputation: 567

Why can the second for loop argument not be equal to number?

I get this: Fatal error: Out of memory:

    <?php
    // Create an array and push 5 elements on to it, then 
    // print the number of elements in your array to the screen
$numbers = array(1,2,3);

for($i=4;$i=8;$i++){
    array_push($numbers,$i);
}
    print count($numbers);
    ?>

But if I change $i=8 to $i<9 it works.

What's up with that?

Upvotes: 0

Views: 40

Answers (2)

Phiter
Phiter

Reputation: 14992

Your for loop is wrong.

           v
for($i=4;$i=8;$i++){

You're setting $i as 8 instead of comparing it to 8.

To compare values, you must use ==

for($i=4;$i==8;$i++){

Also, to clarify, you're getting the error "out of memory" because the loop is running infinitely, which is causing your application to use all available space in memory, causing it to break.

Upvotes: 1

Louren&#231;o Biselli
Louren&#231;o Biselli

Reputation: 59

Maybe you can trie to put to equal instead of one, like "==".

Hope it helps.

Upvotes: 0

Related Questions