How can the validity of a URL be determined using filter_var in PHP?

To determine the validity of a URL using filter_var in PHP, you can use the FILTER_VALIDATE_URL filter option. This will check if a given URL is valid according to the URL syntax rules. If the URL is valid, filter_var will return the URL. If the URL is not valid, filter_var will return false.

$url = "https://www.example.com";

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "Valid URL";
} else {
    echo "Invalid URL";
}