-5

Trying to check a list. Need some help with the foreach loop. Can't seem to understand the documentation for it.

  foreach ($i = 0; $i <= $List; $i++)
     {
         foreach($ii = 0; $ii2 <= $List 2; ii++)
         {

          }
        // something
     }

3 Answers3

1

This is not a correct foreach syntax. Use for loop instead.

$a = array("marx","engels","lenin","stalin","mao zedong");
$a_size = count($a);
for($i=0; $i<$a_size; $i++) {
  echo $a[$i];
}

Foreach usage.

$comrades = array("marx","engels","lenin","stalin","mao zedong");
foreach($comrades as $comrade) {
  echo "Comrade ".$comrade;
}
marmeladze
  • 6,468
  • 3
  • 24
  • 45
0

Thats not a foreach loop, thats a for-loop with syntax error. That would be a right FOR-Loop:

foreach ($i = 0; $i <= $List; $i++) {
    foreach($a = 0; $a <= $List2; a++) { // You can't have a whitespace between "List and 2"
        // Do something inside inner loop
    }
    // Staff in outer loop      
}

http://php.net/manual/de/control-structures.for.php

If you wan't a foreach-loop:

foreach($your_array as $element) {
    // This loop is going to be executed for each element of your array. You can access the actual value by $element
}

http://php.net/manual/de/control-structures.foreach.php

Twinfriends
  • 1,972
  • 1
  • 14
  • 34
0
$i = 0 // defines starting value
$i <= $List // condition (while true loop goes on)
$i++ // change of the loop variable that at some point will end the loop

inside this loop is nested loop I guess in nested loop it should be "$List"2 not "$List 2"

NoOorZ24
  • 2,914
  • 1
  • 14
  • 33