Closed
Description
Description
The following code https://3v4l.org/QIUQr :
<?php
$testvectors = [
'null' => null,
'empty string' => '',
/*
'one space' => ' ',
'four spaces' => ' ',
'one tab' => "\t",
'four tabs' => "\t\t\t\t",
'one newline' => "\n",
'four newlines' => "\n\n\n\n",
'one carriage return' => "\r",
'four carriage returns' => "\r\r\r\r",
'one colon' => ':',
'four colons' => '::::',
'one @ sign' => '@',
'four @ signs' => '@@@@',
'one slash' => '/',
'four slashes' => '////',
'one backslash' => '\\',
'four backslashes' => '\\\\\\\\',
'one percent' => '%',
'four percents' => '%%%%',
'one plus' => '+',
'four plusses' => '++++',
'one question mark' => '?',
'four question marks' => '????',
'one # sign' => '#',
'four # signs' => '####',
*/
];
foreach($testvectors as $name => $value) {
echo '--- Testvector '.$name.' ---'.PHP_EOL;
$result = parse_url($value);
if ($result === false) {
echo 'FALSE'.PHP_EOL;
} else {
echo 'NOT FALSE'.PHP_EOL;
var_dump($result);
}
echo PHP_EOL;
}
Resulted in this output:
--- Testvector null ---
Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /in/QIUQr on line 39
NOT FALSE
array(1) {
["path"]=>
string(0) ""
}
--- Testvector empty string ---
NOT FALSE
array(1) {
["path"]=>
string(0) ""
}
But I expected this output instead:
--- Testvector null ---
Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /in/QIUQr on line 39
FALSE
--- Testvector empty string ---
FALSE
Background:
Often url's are configuration settings. And sometimes they are not set, e.g. NULL or ''. I assumed (never assume anything, I know) parse_url() would return false for clearly invalid url's like NULL and ''.
For more background fun, I got triggered to investigate this by a deprecation notice parse_url(): Passing null to parameter #1 ($url) of type string is deprecated
in the following function.
public static function addQueryParametersToUrl($url, $queryParms)
{
$parsed = parse_url($url);
if (false === $parsed) {
throw new Exception('Invalid url: '.$url);
}
$query = '';
foreach ($queryParms as $name => $value) {
$query .= '&'.rawurlencode($name).'='.rawurlencode($value);
}
if (0 == strlen($query)) {
return $url;
}
if (!isset($parsed['query'])) {
$parsed['query'] = substr($query, 1);
} else {
$parsed['query'] .= $query;
}
return self::unparse_url($parsed);
}
PHP Version
-