How to get URL query string parameters with PHP?

Spread the love

Sometimes, we want to get URL query string parameters with PHP.

In this article, we’ll look at how to get URL query string parameters with PHP.

How to get URL query string parameters with PHP?

To get URL query string parameters with PHP, we can use the parse_url function.

For instance, we write

$url = http://www.example.com/page.php?x=100&y=200';
$url_components = parse_url($url); 
parse_str($url_components['query'], $params); 
echo $params['x'];

to call parse_url with $url to parse the $url string into an associative array with various URL parts.

Then we call parse_str with the $url_components['query'] and $params to put the query parameters into the $params associative array.

And then we get the value of the x query parameter with $params['x'].

Conclusion

To get URL query string parameters with PHP, we can use the parse_url function.

Leave a Reply

Your email address will not be published. Required fields are marked *