How can URLs be checked for validity in PHP scripts?

To check the validity of URLs in PHP scripts, you can use the filter_var function with the FILTER_VALIDATE_URL filter. This function will return true if the URL is valid and false if it is not.

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

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