In this tutorial, we are going to see how to check if a URL is valid in PHP. When a URL is submitted, it is very important to check if this URL is valid or not before performing any action. Here we are going to see how to check if a URL is valid in PHP using filter_var() function with FILTER_VALIDATE_URL filter. Use the following code to check if the URL is valid.
Validate URL in PHP filter_var() function
<?php
$url = "https://google.com";
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
echo("Url is valid");
} else {
echo("Url is not valid");
}
?>
Output of this program will be
Url is valid.
Practice and Learn PHP mcqs here. PHP MCQ Questions