Home >Backend Development >PHP Tutorial >How Do I Retrieve URL Parameters in PHP?
Retrieving URL Parameters in PHP
In PHP, you can pass parameters in a URL using the ? character followed by the parameter name and value, e.g.:
http://localhost/dispatch.php?link=www.google.com
To retrieve a specific parameter value, you can access the $_GET superglobal array, which contains all GET parameters from the URL. To access the link parameter from the example URL, use:
echo $_GET['link'];
However, this code may fail if the link parameter is not present in the URL. To avoid this, use:
if (isset($_GET['link'])) { echo $_GET['link']; } else { // Handle missing parameter }
Alternatively, use the filter_input() function for parameter retrieval:
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);
Or, since PHP 7.0, use the null coalescing operator:
echo $_GET['link'] ?? 'Fallback value';
The above is the detailed content of How Do I Retrieve URL Parameters in PHP?. For more information, please follow other related articles on the PHP Chinese website!