How can the $_SERVER variable be utilized in PHP to extract information for breadcrumb navigation?

When creating breadcrumb navigation in PHP, the $_SERVER variable can be utilized to extract information such as the current URL or request URI. This information can then be used to dynamically generate breadcrumb links based on the current page's hierarchy.

// Get the current URL
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

// Split the URL into an array of segments
$url_segments = explode('/', $current_url);

// Generate breadcrumb links based on the URL segments
$breadcrumbs = array();
$breadcrumbs[] = '<a href="/">Home</a>'; // Home link
$breadcrumb_path = '';
foreach($url_segments as $segment){
    $breadcrumb_path .= $segment . '/';
    $breadcrumbs[] = '<a href="' . $breadcrumb_path . '">' . ucfirst($segment) . '</a>'; // Capitalize segment names
}

// Output the breadcrumb links
echo implode(' > ', $breadcrumbs);