Home > Backend Development > PHP Tutorial > Why is my PHP code returning an empty result when trying to retrieve GET URL parameters using `$_GET['link']`?

Why is my PHP code returning an empty result when trying to retrieve GET URL parameters using `$_GET['link']`?

Linda Hamilton
Release: 2024-12-13 22:28:16
Original
894 people have browsed it

Why is my PHP code returning an empty result when trying to retrieve GET URL parameters using `$_GET['link']`?

Retrieving GET URL Parameters in PHP

Question:

When attempting to access a URL passed as a parameter via a URL form (e.g., http://localhost/dispatch.php?link=www.google.com), the code returns an empty result using $_GET['link'];. What is the issue?

Answer:

The $_GET array is a superglobal that stores GET parameters. However, it functions as a variable rather than a language construct. To access its values, use the echo statement:

echo $_GET['link'];
Copy after login

To handle cases where the parameter is not present, you can employ conditional statements:

if (isset($_GET['link'])) {
    echo $_GET['link'];
} else {
    // Fallback behavior here
}
Copy after login

Alternatively, you can use the filter extension for validation and error handling:

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

Finally, the null coalescing operator (PHP 7.0 onwards) provides concise fallback behavior:

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

The above is the detailed content of Why is my PHP code returning an empty result when trying to retrieve GET URL parameters using `$_GET['link']`?. 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