Here's a solution using a ternary statement, keeping the code minimal:
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
This is the smallest and easiest way to do this, assuming one's web server is using the standard port 443 for HTTPS.