What are some common pitfalls when handling global variables in PHP arrays and how can they be avoided?

One common pitfall when handling global variables in PHP arrays is accidentally overwriting or modifying the global variable unintentionally within a function. To avoid this, it is recommended to use the `global` keyword within the function to explicitly declare the global variable and prevent creating a local variable with the same name.

// Incorrect way of handling global variables in PHP arrays
$globalArray = [];

function addToGlobalArray($value) {
    $globalArray[] = $value; // This creates a new local variable instead of modifying the global variable
}

// Correct way of handling global variables in PHP arrays
$globalArray = [];

function addToGlobalArray($value) {
    global $globalArray;
    $globalArray[] = $value; // Modifies the global variable
}