What is the difference between using a foreach loop and a for loop in PHP when assigning values to an array?

When assigning values to an array in PHP, using a foreach loop is more convenient and readable compared to a traditional for loop. The foreach loop automatically iterates over each element in an array without the need for manual indexing or counting. This makes the code cleaner and less prone to errors. Additionally, foreach loop can handle associative arrays easily, which can be more complex with a for loop.

// Using foreach loop to assign values to an array
$numbers = [1, 2, 3, 4, 5];
$newArray = [];

foreach ($numbers as $number) {
    $newArray[] = $number * 2;
}

print_r($newArray);