What are best practices for handling URL validation in PHP scripts?

When handling URL validation in PHP scripts, it is important to use a reliable method to ensure that the input is a valid URL. One common approach is to use the filter_var function with the FILTER_VALIDATE_URL filter. This function will check if the input is a valid URL according to the URL syntax defined in RFC 2396. By using this method, you can easily validate URLs in your PHP scripts.

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

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