parse_url

FunctionParams and return types changed in PHP 8.0

Parse a URL and return its components.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

parse_url Function synopsis

parse_url(string $url, int $component = -1): array|string|int|false|null

Parameters

$url

Typestring

The URL to parse.

$component

OptionalTypeintDefault value-1

Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when PHP_URL_PORT is given, in which case the return value will be an int).

$component accepts one of the following constant values:

Return value

Typearray|string|int|false|null

On seriously malformed URLs, parse_url may return false.

If the $component parameter is omitted, an associative array is returned. At least one element will be present within the array. Potential keys within this array are:

If the $component parameter is specified, parse_url returns a string (or an int, in the case of PHP_URL_PORT) instead of an array. If the requested component doesn't exist within the given URL, null will be returned. As of PHP 8.0.0, parse_url distinguishes absent and empty queries and fragments:

Previously all cases resulted in query and fragment being `null`.

Note that control characters (cf. ctype_cntrl) in the components are replaced with underscores (_).

Changes to the parse_url Function

PHP 8.0

  • Return type added: array|string|int|false|null
  • Parameter type added for parameter #1 ($url): string
  • Parameter type added for parameter #2 ($component): int
  • Parameter default value added for position #2 ($component): -1
- parse_url($url, $component)
+ parse_url(string $url, int $component = -1): array|string|int|false|null

parse_url Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes