Php - foreach

> Procedural Languages > Php

1 - About

foreach is a loop structure over an array

For each

Advertising

3 - Example

$colors = array('red', 'blue', 'green', 'yellow');
 
foreach ($colors as $color) {
    echo "Do you like $color?\n";
}
 
 
// Changing element in the loop
foreach ($colors as $key => $color) {
    $colors[$key] = strtoupper($color);
}