What are some potential pitfalls when using arrays in PHP for variable initialization and assignment?

One potential pitfall when using arrays in PHP for variable initialization and assignment is accidentally overwriting existing array elements when reassigning values. To avoid this issue, you can use array functions like array_merge() or the shorthand operator += to add new elements to an existing array without losing the previous data.

// Pitfall: Overwriting existing array elements
$colors = ['red', 'green', 'blue'];
$colors = ['yellow', 'orange', 'purple']; // This will overwrite the original $colors array

// Solution: Use array_merge() or += to add new elements without overwriting
$colors = ['red', 'green', 'blue'];
$additionalColors = ['yellow', 'orange', 'purple'];

$colors = array_merge($colors, $additionalColors);
// OR
$colors += $additionalColors;

print_r($colors); // Output: Array ( [0] => red [1] => green [2] => blue [3] => yellow [4] => orange [5] => purple )