开发者

What is wrong with this while loop?

开发者 https://www.devze.com 2023-02-26 08:03 出处:网络
Why does 开发者_如何学Gothis 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.

Why does 开发者_如何学Gothis 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>';

}


$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.


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.


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


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++;
}


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


$i = -1;

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

or

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消