In what ways can global variables impact the stability and security of PHP scripts, especially when used in functions like fsockopen?

Global variables can impact the stability and security of PHP scripts by introducing unexpected behavior, making it harder to track and debug issues, and potentially leading to security vulnerabilities if not properly sanitized. To mitigate these risks, it is recommended to avoid using global variables whenever possible and instead pass variables as parameters to functions.

function connectToServer($host, $port) {
    $socket = fsockopen($host, $port, $errno, $errstr, 30);
    
    if (!$socket) {
        die("Error: $errstr ($errno)");
    }
    
    // Rest of the code to interact with the server
    
    fclose($socket);
}

// Usage
$host = "example.com";
$port = 80;
connectToServer($host, $port);