What is the "less code needed" way to get parameters from a URL query string which is formatted like the following?
www.mysite.com/category/subcategory?myqueryhash
Output should be: myqueryhash
I am aware of this approach:
www.mysite.com/category/subcategory?q=myquery <?php echo $_GET['q']; //Output: myquery ?>
What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.
Both parameters and query string contain key-value pairs. In a POST request, parameters can appear in the URL itself, but also in the datastream (as known as content). Query string is always a part of the URL. Parameters can be buried in form-data datastream when using POST method so they may not appear in the URL.
A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.
$_SERVER['QUERY_STRING']
contains the data that you are looking for.
DOCUMENTATION
The PHP way to do it is using the function parse_url, which parses a URL and return its components. Including the query string.
Example:
$url = 'www.mysite.com/category/subcategory?myqueryhash'; echo parse_url($url, PHP_URL_QUERY); # output "myqueryhash"
Full documentation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With