Explanation:The foreach loop is used to iterate over the entire array from start to finish. There are two forms of the foreach loop, which are as follows:

foreach(array_expression as $value)

{

//Statement;

}

or

foreach(array_expression as $key => $value)

{

//Statement;

}

In the first form, foreach loop iterates over the array that is given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer increases by one.

where, in the second form of the foreach loop, on each iteration, the value of the current element is assigned to $value. Also, the current element's key will be assigned to the variable $key on each iteration and the internal array pointer increases by one. The comments explain the working of the PHP script: