Lokang 

PHP and MySQL

Foreach

In PHP, the foreach loop is used to iterate over the elements of an array. It allows you to loop through each element of an array and perform a set of actions on each element.

Here is the basic syntax of the foreach loop:

foreach ($array as $value) {
  // code to be executed
}

The $array variable is the array that you want to loop through, and the $value variable is a placeholder for the current element of the array. The loop will execute once for each element of the array, with the value of the current element being assigned to $value on each iteration.

Here is an example of using the foreach loop to loop through an array of numbers and print each element to the screen:

$numbers = array(1, 2, 3, 4, 5);
foreach ($numbers as $number) {
  echo $number . "\n";
}

This would output:

1
2
3
4
5

You can also use the foreach loop to iterate over the elements of an associated array, by specifying two variables: one for the key and one for the value. The key variable will hold the key of the current element, and the value variable will hold the value.

Here is an example of using the foreach loop to loop through an associated array and print the key-value pairs:

$student = array(
  "first_name" => "John",
  "last_name" => "Doe",
  "age" => 25
);
foreach ($student as $key => $value) {
  echo "$key: $value\n";
}
This would output:
first_name: John
last_name: Doe
age: 25