Home >Backend Development >PHP Tutorial >How Do I Retrieve URL Parameters in PHP?

How Do I Retrieve URL Parameters in PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-28 02:59:15348browse

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!

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