Home >Backend Development >PHP Tutorial >How Can I Efficiently Access URL Query String Parameters in PHP?
Accessing URL Query String Parameters with Minimal Code
In this question, the asker seeks a concise method to extract the query string parameter from a URL. Typically, to retrieve the parameter using PHP, one would utilize the $_GET superglobal. However, an alternative approach requires less code.
To obtain the desired result, we can utilize the $_SERVER superglobal. Specifically, the $_SERVER['QUERY_STRING'] element contains the complete query string, including the parameter.
Code Example:
// Example URL: www.mysite.com/category/subcategory?myqueryhash // Get the query string $query_string = $_SERVER['QUERY_STRING']; // Extract the parameter value $parameter_value = strstr($query_string, '?').substr(strstr($query_string, '?'), 1); // Output the result echo $parameter_value; // Output: myqueryhash
Reference Documentation:
The above is the detailed content of How Can I Efficiently Access URL Query String Parameters in PHP?. For more information, please follow other related articles on the PHP Chinese website!