How can global variables be effectively used in PHP functions to manipulate arrays like $xml in the code snippet provided?

Global variables can be effectively used in PHP functions to manipulate arrays like $xml by declaring the variable as global within the function. This allows the function to access and modify the global variable directly. However, it is important to use global variables judiciously to avoid potential conflicts and maintain code readability.

$xml = []; // Global array variable

function manipulateXmlArray($data) {
    global $xml; // Declare $xml as global within the function
    // Manipulate $xml array using $data
    $xml[] = $data; // Example: Add $data to $xml array
}

// Usage example
manipulateXmlArray("data1");
manipulateXmlArray("data2");

print_r($xml); // Output the manipulated $xml array