How can PHP's filter_var function be used to validate URLs?

To validate URLs using PHP's filter_var function, you can use the FILTER_VALIDATE_URL filter option. This will check if a given string is a valid URL according to the URL syntax defined in RFC 2396. If the URL is valid, the function will return the URL, otherwise it will return false.

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

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