What are some best practices for ensuring the correctness of a URL's syntax in PHP, taking into account both the host and other components?

When working with URLs in PHP, it is important to ensure that the syntax of the URL is correct, including the host and other components like the path, query parameters, and fragment. One way to validate the correctness of a URL's syntax is by using the filter_var function with the FILTER_VALIDATE_URL filter. This function will check if the URL is valid according to RFC standards.

$url = "https://www.example.com/path/to/page?param1=value1#section";

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "URL is valid";
} else {
    echo "URL is not valid";
}