PHP Foreach The loop is used to print all the data in the arrays. PHP Foreach In each iteration of the loop, the data in the array is printed.

PHP Foreach syntax is as follows.

foreach ($array as $value) {
    // Çalıştırılacak kodlar)
}

The array to be returned is added instead of $array, and the variable in which the data will be stored is added instead of $value. PHP Foreach In the cycle, codes to be executed in each iteration are written into the code scope. If array keys will also be returned, the syntax is as follows. The variable where the keys will be stored is written instead of the $key variable.

foreach ($array as $key => $value) {
    // Çalıştırılacak kodlar)
}

Using PHP Foreach

PHP Foreach In the example below in the loop, we created an array named $colors and gave the color values. Then with the foreach statement PHP ForeachWe created the loop and wrote the array name as $colors. We used the as expression and specified the variable name where the array data will be stored as $value. Each time the loop returned, we printed the data of the current $value variable with echo. As a result, you can save all the data in the array with a short and clean method, PHP Foreach We printed it through the loop.

PHP Foreach In the example below, we created an array named $days in the loop and gave the description of the day of the week as the key and the day values ​​as the value. Then with the foreach statement PHP ForeachWe created the loop and wrote the array name as $days. We used the as expression and specified the variable in which the keys of the array will be stored, namely the $key variable. Then, we specified the variable name in which the array data will be stored, in the form of $value. Each time the loop returned, we printed the data of the current $value variable with echo. As a result, you can save all the data in the array with a short and clean method, PHP Foreach We printed it through the loop.

$colors = array(
    'mavi',
    'kırmızı',
    'sarı',
    'turuncu',
    'yeşil',
    );
foreach ($colors as $value) {
    echo $value . '
';
}
/*
 * Çıktı: 
mavi
kırmızı
sarı
turuncu
yeşil
*/
$days = array(
    'haftanın ilk günü'        => 'pazartesi',
    'haftanın ikinci günü'     => 'salı',
    'haftanın üçüncü günü'     => 'çarşamba',
    'haftanın dördüncü günü'   => 'perşembe',
    'haftanın beşinci günü'    => 'cuma',
    'haftasonunun ilk günü'    => 'cumartesi',
    'haftasonunun ikinci günü' => 'pazar',
);
foreach ($days as $key => $value) {
    echo $key . ': ' . $value . '
';
}
/*
 * Çıktı:
haftanın ilk günü: pazartesi
haftanın ikinci günü: salı
haftanın üçüncü günü: çarşamba
haftanın dördüncü günü: perşembe
haftanın beşinci günü: cuma
haftasonunun ilk günü: cumartesi
haftasonunun ikinci günü: pazar
*/

PHP Foreach In the example below, we created an array named $age in the loop and gave names as keys and age values as values. Then with the foreach statement PHP ForeachWe created the loop and wrote the array name as $age. We used the as expression and specified the variable in which the keys of the array will be stored, namely the $key variable. Then, we specified the variable name in which the array data will be stored, in the form of $value. Each time the loop returned, we printed the data of the current $value variable with echo. As a result, you can save all the data in the array with a short and clean method, PHP Foreach We printed it through the loop.

$age = array(
    'Burak' => 25,
    'Ece'   => 22,
    'Berk'  => 19,
    'Sevda' => 29,
);
foreach ($age as $key => $value) {
    echo $key . ': ' . $value . '
';
}
/*
 * Çıktı:
Burak: 25
Ece: 22
Berk: 19
Sevda: 29
*/

PHP Foreach Alternative Syntax

Many control structures in PHP, including Foreach, support alternative syntax. PHP Foreach alternative syntax: colon instead of opening curly braces, endforeach instead of closing curly braces; We use the expression. PHP Foreach alternative syntax is as follows

foreach ($array as $value):
    // Çalıştırılacak kodlar
endforeach;

PHP Foreach alternative syntax in a real-life exampleLet's use it. PHP Foreach in the example below We created an array named $summer to use in the loop and gave the summer months as the value. Then with the foreach statement PHP Foreach We created the cycle. We added our array named $summer and the variable $month, where the values ​​of the array will be stored, along with the as statement. Since we are using alternative syntax, we added a colon instead of opening curly brackets. In this way PHP Foreach We have started code coverage. We printed the values ​​at each iteration with echo. Finally, instead of closing curly braces, endforeach; in the words PHP Foreach We have finalized the code coverage of the loop.

$summer = array(
  'Haziran',
  'Temmuz',
  'Ağustos',
    );
foreach ($summer as $month) {
    echo $month . '
';
}
/*
 * Çıktı: 
Haziran
Temmuz
Ağustos
*/