How to validate a URL in PHP

When a URL is submitted from a form input by the user, it is very important to check this URL is valid or not before taking any action on it. Here we’ll provide the simple PHP code to validate a URL in PHP. Using filter_var() function with FILTER_VALIDATE_URL Filter, we can easily validates a URL in PHP.

Use the following code to check if the variable ($url) is a valid URL.


if (!
filter_var($urlFILTER_VALIDATE_URL) === false) {
"$url is a valid URL");
} else {
"$url is not a valid URL");


  1. Laurent Said...
  2. Campervan Hire Adelaide Said...

Leave a reply