Current Location: Home> Latest Articles> parse_url returns the difference between NULL and FALSE

parse_url returns the difference between NULL and FALSE

gitbox 2025-05-26

In PHP, the parse_url function is a very practical tool for parsing URLs and returning their components, such as protocols, host names, paths, etc. The basic usage of the function is as follows:

 $url = "https://gitbox.net/path?query=1";
$parts = parse_url($url);
print_r($parts);

This code outputs various components of the URL, such as:

 Array
(
    [scheme] => https
    [host] => gitbox.net
    [path] => /path
    [query] => query=1
)

However, sometimes you may encounter situations where parse_url returns NULL or FALSE , and you will be puzzled: What is the difference between them? This article will explain in detail the meaning and applicable scenarios of these two return values.

parse_url Returns FALSE

In PHP 7.3 and earlier, if the incoming URL format is extremely wrong, parse_url will return FALSE , indicating that parsing failed.

 $url = "ht!tp:::/invalid-url";
$result = parse_url($url);
var_dump($result);  // bool(false)

The URL format here is obviously not in compliance with the specifications, and the function cannot be parsed normally, so FALSE is returned.

Summarize:

  • Returning FALSE usually means that the passed string cannot be recognized as a legitimate URL or URI.

  • It may be because the string contains unauthorized characters or is formatted incorrectly.

parse_url Returns NULL

In fact, the parse_url function does not explicitly specify in the official documentation to return NULL . However, in some specific cases, such as calling parse_url and passing in an empty string or non-string type, NULL may appear.

 $url = "";
$result = parse_url($url);
var_dump($result);  // NULL or empty array,depending on PHP Version

In addition, if a specific part of the URL is requested to parse and this part does not exist in the URL, the corresponding part of the return value will be NULL .

 $url = "https://gitbox.net";
$host = parse_url($url, PHP_URL_HOST);
var_dump($host);  // string(9) "gitbox.net"

$path = parse_url($url, PHP_URL_PATH);
var_dump($path);  // NULL,because URL No path part

The NULL returned here is a result for a specific part, indicating that the part does not exist.

Summarize:

  • Returning NULL may be a request for a URL component, which does not exist.

  • It may also be that an empty string or irregular parameter has been passed in, and the specific performance depends on the PHP version.

Summary and suggestions

Return value meaning Typical scenarios
FALSE The URL is incorrect and cannot be parsed The input string contains illegal characters or structural exceptions
NULL The requested URL component does not exist or an empty string Request a part that does not exist, or pass an empty string

In actual development, it is recommended:

  1. First check whether the URL string is empty or the format is obviously incorrect to avoid passing in illegal strings.

  2. When calling parse_url , if only some fields are needed, be sure to determine whether the return result is NULL to avoid the program's error.

  3. Before parsing, you can use regular or filtering functions to initially verify the URL structure to improve program robustness.

For example:

 $url = "https://gitbox.net";

$host = parse_url($url, PHP_URL_HOST);
if ($host === null) {
    echo "URL None in host part\n";
} else {
    echo "Host: $host\n";
}

Through reasonable judgment, logical errors caused by NULL or FALSE can be avoided.