Computer >> Computer tutorials >  >> Programming >> PHP

FILTER_VALIDATE_URL constant in PHP


The FILTER_VALIDATE_URL constant validates a URL.

Flags

  • FILTER_FLAG_SCHEME_REQUIRED − URL must be RFC compliant.

  • FILTER_FLAG_HOST_REQUIRED − URL must include host name.

  • FILTER_FLAG_PATH_REQUIRED −URL must have a path after the domain name.

  • FILTER_FLAG_QUERY_REQUIRED −URL must have a query string.

Return

The FILTER_VALIDATE_URL constant does not return anything.

Example

<?php
$url = "https://www.example.com";
if (filter_var($url, FILTER_VALIDATE_URL)) {
   echo("Valid URL!");
} else {
   echo("Invalid URL!");
}
?>

Output

The following is the output.

Valid URL!

Let us see another example.

Example

<?php
$url = "examplecom";
if (filter_var($url, FILTER_VALIDATE_URL)) {
   echo("Valid URL!");
} else {
   echo("Invalid URL!");
}
?>

Output

Here is the output.

Invalid URL!