How can you extract the subdomain from the browser URL using PHP?

To extract the subdomain from a browser URL using PHP, you can use the parse_url function to get the hostname from the URL and then split the hostname by dots to extract the subdomain.

$url = "http://subdomain.example.com";
$hostname = parse_url($url, PHP_URL_HOST);
$subdomain = explode('.', $hostname)[0];
echo $subdomain;