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

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Owl City
  • 35
  • 1
  • 6
  • 1
    Wow, does it really need 4 exact same answers? – Charlotte Dunois Aug 15 '15 at 05: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.](http://stackoverflow.com/q/31016660/2089760) – DDPWNAGE Aug 15 '15 at 06:06
  • @DDPWNAGE Yes, completely agree with you thanks for your explanation. have updated my answer. – Ankur Tiwari Aug 15 '15 at 06: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. – Josua Marcel C Aug 15 '15 at 07:00

7 Answers7

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++;
 }
 ?>
gommb
  • 1,121
  • 1
  • 7
  • 21
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++
}
?>
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
  • Can you do that without using $i++? – Owl City Aug 15 '15 at 05:53
  • Check below code @Sunny have posted code without while. Without changing $i variable it is not possible to break the loop. – Brn.Rajoriya Sep 02 '15 at 13:34
  • 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 " $propertyAmenities remove
    "; IT is not looping the items under the same listingId until there are no more available. It is only displaying 1, the first one.
    – Rookie Recruits Aug 17 '18 at 20:28
  • Make sure you have more entries under same `listingId` – Muhammad Hassaan Aug 18 '18 at 05:04
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.

Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40
1

You can also use foreach to do this

$animals = array('pig', 'chicken', 'cow', 'snake');
foreach($animals as $animal)
echo "<li>{$animal}</li>";
Sunny
  • 119
  • 2
  • 18
0
<?php 
  $animals = array('pig', 'chicken', 'cow', 'snake');
  $i = 0;
  $max = sizeof($animals);
  while ($i < max) {
    echo "<li>$animals[$i]</li>";
    i++;
  }
?>
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
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

Wee Zel
  • 1,294
  • 9
  • 11
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++;
    }
?>