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

When handling URL validation in PHP, it is important to use a combination of built-in functions and regular expressions to ensure that the URL is valid. One common approach is to use the filter_var() function with the FILTER_VALIDATE_URL filter to check if the URL is well-formed. Additionally, you can use regular expressions to further validate the URL structure.

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

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