chromedude
chromedude

Reputation: 4302

What is wrong with this while loop?

Why does this loop stop my page from working? without the loop part the code runs fine. With the loop it doesn't execute any of the page and does not print an error.

$i = 0;  
while($i < 10) {

echo '<div class="feedItem">
<div class="itemDetails">
<div class="innerDetails">
<span class="itemType">type</span>
<span class="itemDate">date</span>
</div>
</div>
<span class="itemTitle">test</span>
<span class="itemCreator">by <a href="http://blankit.co.cc">chromedude</a></span>
<div class="itemTagCloud">
<span class="itemSubject">English</span>
<span class="itemTag">Test</span>
<span class="itemTag">Poem</span>
<span class="itemTag">Edgar Allen Poe</span>
</div>
</div>';

}

Upvotes: 1

Views: 89

Answers (7)

Rad&#39;Val
Rad&#39;Val

Reputation: 9241

You should add $i++ at the end of the code, currently $i it's ALWAYS smaller then 10 => infinite loop..

Upvotes: 0

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

$i = -1;

while (++$i < 10) {
  // your code
  }

or

while ($i < 10) {
  // your code
  $i++;
  }

Upvotes: 0

RageD
RageD

Reputation: 6823

$i is always less than 10 (equal to 0) since you're never changing its value. If you simply want that block to be printed 10 times, try adding $i++; at the bottom of your loop. This way, it increments $i by one every iteration.

Upvotes: 4

Ry-
Ry-

Reputation: 225124

You never increment $i! The loop will never complete. Add $i++ at the end of the loop.

Upvotes: 0

Jared Farrish
Jared Farrish

Reputation: 49218

You need to increment $i:

$i = 0;  
while($i < 10) {
    echo '<div class="feedItem">
    <div class="itemDetails">
    <div class="innerDetails">
    <span class="itemType">type</span>
    <span class="itemDate">date</span>
    </div>
    </div>
    <span class="itemTitle">test</span>
    <span class="itemCreator">by <a href="http://blankit.co.cc">chromedude</a></span>
    <div class="itemTagCloud">
    <span class="itemSubject">English</span>
    <span class="itemTag">Test</span>
    <span class="itemTag">Poem</span>
    <span class="itemTag">Edgar Allen Poe</span>
    </div>
    </div>';

    $i++;
}

Upvotes: 0

Tony The Lion
Tony The Lion

Reputation: 63250

add $i = $i + 1 before your last bracket.

Upvotes: 0

halfdan
halfdan

Reputation: 34234

You don't increment $i inside the loop. This way your condition will always be true and the while loop will never exit.

Add $i++ or $i = $i + 1 just before the closing curly brace and you'll be fine.

Upvotes: 0

Related Questions