What is the best approach to convert relative links to absolute links in PHP?

When dealing with relative links in PHP, it is important to convert them to absolute links to ensure they work correctly across different pages and domains. One way to achieve this is by using the PHP function `parse_url()` to extract the base URL and then concatenate it with the relative link to form an absolute link.

function convertRelativeToAbsolute($relativeLink) {
    $baseURL = 'http://www.example.com'; // Your base URL here
    $parsedURL = parse_url($baseURL);
    
    if (substr($relativeLink, 0, 1) == '/') {
        return $parsedURL['scheme'] . '://' . $parsedURL['host'] . $relativeLink;
    } else {
        return $baseURL . '/' . $relativeLink;
    }
}

// Example usage
$relativeLink = '/path/to/page';
$absoluteLink = convertRelativeToAbsolute($relativeLink);
echo $absoluteLink;