检查URL方案是HTTP还是HTTPS

I'm using the following code to add http:// to the URL.

(substr(strtolower($url), 0, 7) == 'http://'?"":"http://").$url

but how can I check whether the original URL contains https? I don't want to use an OR clause.

preg_match("@^https?://@", $url)

Use preg_match and a regular expression on your url :

preg_match(^http(s)?://);

If it returns true, then your URL is ok, whether it uses http of https.

echo parse_url($url, PHP_URL_SCHEME);
strncmp($url, 'https:', 6) === 0
!empty($_SERVER['HTTPS']) ? 'https' : 'http'