Home >Backend Development >PHP Tutorial >How Can I Efficiently Access URL Query String Parameters in PHP?

How Can I Efficiently Access URL Query String Parameters in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-12-15 01:32:14390browse

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:

  • [$_SERVER - Manual](https://www.php.net/manual/en/reserved.variables.server.php)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn