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

How Do I Retrieve URL Parameters in PHP?

Mary-Kate Olsen
Release: 2024-12-28 02:59:15
Original
345 people have browsed it

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
Copy after login

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'];
Copy after login

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
}
Copy after login

Alternatively, use the filter_input() function for parameter retrieval:

echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);
Copy after login

Or, since PHP 7.0, use the null coalescing operator:

echo $_GET['link'] ?? 'Fallback value';
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template