What are the advantages of using a function to automatically parse URLs in PHP?

When working with URLs in PHP, it can be tedious and error-prone to manually parse them to extract specific components like the protocol, host, path, query parameters, etc. By using a function to automatically parse URLs, you can ensure accuracy and save time in your code. This function can take a URL string as input and return an array or object with all the parsed components, making it easier to work with URLs in your PHP applications.

function parse_url_components($url) {
    $parsed_url = parse_url($url);
    
    return $parsed_url;
}

$url = "https://www.example.com/path/to/page?query=123";
$url_components = parse_url_components($url);

// Access individual components like this
echo $url_components['scheme']; // Output: https
echo $url_components['host']; // Output: www.example.com
echo $url_components['path']; // Output: /path/to/page
echo $url_components['query']; // Output: query=123