0
$evens = array(1, 2, 3, 4, 5, 6);

foreach ($evens as $index=>$value){

    if ($value % 2 == 0){

    echo "<br> Even numbers are :$value";
    }

}

Hello folks,

I am just reading about foreach loops, I'm just wondering what means this:

$evens as $index=>$value

$evens = my array $value = random named (by me) variable that contains results but what is $index? Is that reserved keyword in PHP or ... ?

Peter
  • 37
  • 6
  • http://php.net/manual/en/control-structures.foreach.php – Lawrence Cherone Feb 21 '18 at 14:23
  • 2
    Arrays in PHP are all associative. If you don't provide the keys then they are generated starting from 0 automatically. `$evens` is exactly equivalent to `array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6)`. – Phylogenesis Feb 21 '18 at 14:23
  • See also [this page](http://php.net/manual/en/language.types.array.php). – Phylogenesis Feb 21 '18 at 14:24
  • array is consist of key and value so divide $evens array into key and value and it is an easy way to get your desired value from array. – Zaheer Abbas Feb 21 '18 at 14:26
  • 1
    Read about PHP [arrays](http://php.net/manual/en/language.types.array.php), [variables](http://php.net/manual/en/language.variables.php) (*"what is $index? Is that reserved keyword in PHP"* -- no, it is a [variable](http://php.net/manual/en/language.variables.basics.php)) and about [`foreach`](http://php.net/manual/en/control-structures.foreach.php). – axiac Feb 21 '18 at 14:26
  • 2
    Please dont ask what `%` does without researching first. – Lawrence Cherone Feb 21 '18 at 14:26
  • I didn't ask what % does, I just asked for $index. Thank you folks. – Peter Feb 21 '18 at 14:28

0 Answers0