In php, if I wanted to parse a URL, such as www.site.com/index.php?foo=bar, I can do with the _POST variable. I can retrieve bar
by _POST['foo']
.
But what if my URL is something like www.site.com/index.php?foo&bar
. i.e., I don't have the arguments as key-value pairs as in the first case. How to parse this in php?
Use
parse_url
.Quoted from php.net:
Then, use
parse_str
function to parse the query parameters part.It's a misconception that requesting a URL like
would give you
bar
in$_POST['bar']
. Url parameters will populate$_GET
. Anything that's supposed to show up in$_POST
has to be submitted in the Request body. See How are parameters sent in an HTTP POST request? for some details.With that clarified, empty URL parameters are not a problem at all. A URL like
will populate
$_GET['foo']
and$_GET['bar']
with empty values.