How can the presence of $_SERVER['HTTPS'] affect URL generation in PHP?

When generating URLs in PHP, the presence of $_SERVER['HTTPS'] can affect whether the generated URL should use 'http://' or 'https://' as the protocol. To ensure that URLs are generated correctly, you can check the value of $_SERVER['HTTPS'] and use it to determine the protocol to use in the generated URLs.

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $url;