1
<?php 
$animals = array('pig', 'chicken', 'cow', 'snake');
$i = 0;
while ($i < 10)
{
    echo "<li>$animals[$i]</li>";
}
?>

I just wanna see the pig, chicken, cow and snake in list item.. but what happen is it just looping the word pig infinitely...

4
  • 1
    Wow, does it really need 4 exact same answers? Commented Aug 15, 2015 at 5:47
  • As @AnkurTiwari pointed out, $i++; is missing, and you should modify your loop statement to while ($i < count($animals)) { ... }. The count($animals) statement was also in Ankur's answer, but there wasn't any explanation to that or $i++;. $i++ adds one to $i so that $i doesn't forever stay at 0. Also, to prevent out-of-bounds undefined behavior evils, I used count($animals). Don't go undefined. Don't do it. Commented Aug 15, 2015 at 6:06
  • @DDPWNAGE Yes, completely agree with you thanks for your explanation. have updated my answer. Commented Aug 15, 2015 at 6:17
  • $i always below than 10 that's why it's doing infinite loop. You should add increment inside while statement to stop the loop. Commented Aug 15, 2015 at 7:00

7 Answers 7

2

You are not adding increment to $i. You can add increment to $i by adding $i++ in your while loop.

<?php 
$animals = array('pig', 'chicken', 'cow', 'snake');
$i = 0;
while ($i < 10)
{
    if (isset($animals[$i]))
    {
        echo "<li>$animals[$i]</li>";
    }
    $i++
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Can you do that without using $i++?
Check below code @Sunny have posted code without while. Without changing $i variable it is not possible to break the loop.
I have a similar code but it is not pulling all of the assoc data with this [id] Can someone help take a look and see what is wrong with this? $res = $dbCon->query("SELECT * FROM listingAmenities WHERE listingId = '$listingId' ORDER BY id DESC"); while($data = $res->fetch_assoc()){ $propertyAmenities = $data['propertyAmenities']; echo "<span class=\"facBtn\"> $propertyAmenities <a href=\"\" class=\"removeFac\" > remove </a></span><br>"; IT is not looping the items under the same listingId until there are no more available. It is only displaying 1, the first one.
Make sure you have more entries under same listingId
2

this should loop through the array by using $i++ which increments the $i variable by one.

<?php 
 $animals = array('pig', 'chicken', 'cow', 'snake');
 $i = 0;
while ($i < 4) {
    echo "<li>$animals[$i]</li>";
    $i++;
 }
 ?>

2 Comments

Don't just post code, tell us what you changed and improved.
can you do that without If statement? what I mean is just using while.
1

You can also use foreach to do this

$animals = array('pig', 'chicken', 'cow', 'snake');
foreach($animals as $animal)
echo "<li>{$animal}</li>";

Comments

1

I have added $i++; which is increment the value of $i by one each time in the loop, so we are able to display each element of array also find length of array in initial to avoid array out-of-bound problem.

<?php 
 $animals = array('pig', 'chicken', 'cow', 'snake');
 $i = 0;
 $len = count($animals);
    while ($i < $len) 
    {
       echo "<li>$animals[$i]</li>";
       $i++;
    }

 ?>

Initially count the length of array and then iterate the loop according to length.

1 Comment

Don't just post code, tell us what you changed and improved.
0

i do like a while loop, so here's one more for the mix! :)

<?php
$animals = array('pig', 'chicken', 'cow', 'snake');
while ($animal = next($animals)) {
    echo "<li>$animal[$i]</li>";
}
?>

this uses next() to get each value of the array, this returns false when its gets to the end of the array terminating the while loop. You don't need to control getting elements of the array using $i and you don't need extra if statements inside the loop checking if your element exists

Comments

0
<?php 
  $animals = array('pig', 'chicken', 'cow', 'snake');
  $i = 0;
  $max = sizeof($animals);
  while ($i < max) {
    echo "<li>$animals[$i]</li>";
    i++;
  }
?>

Comments

0

You haven't incremented the $i value so it is always $i = 0; so it always under 10.

Add $i++ to increment it to avoid this, but with this code you get another new problem, because the array is having only 3 elements and you are using 10 in the while loop.

<?php 
    $animals = array('pig', 'chicken', 'cow', 'snake');
    $i = 0;
    while ($i < 10)
    {
        echo "<li>$animals[$i]</li>";
        $i++;
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.