What is the difference between using a foreach loop and a while loop in this context?
When iterating over an array in PHP, using a foreach loop is generally more concise and easier to read compared to a while loop. The foreach loop automatically handles the iteration over each element of the array without the need for manual index tracking. On the other hand, a while loop requires manual index tracking and incrementing, which can lead to more verbose code and potential off-by-one errors.
// Using a foreach loop to iterate over an array
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number . PHP_EOL;
}