PHP 中的 GET URL 参数 [英] GET URL parameter in PHP

查看:37
本文介绍了PHP 中的 GET URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 php 中将 URL 作为 url 参数传递,但是当我尝试获取此参数时,我什么也没得到

我正在使用以下网址形式:

http://localhost/dispatch.php?link=www.google.com

我正在努力解决:

$_GET['link'];

但没有任何回报.有什么问题?

解决方案

$_GET 不是函数或语言结构——它只是一个变量(一个数组).试试:

特别是,它是一个超全局:一个内置的-在由 PHP 填充并在所有范围内可用的变量中(您可以在没有 全局 关键字).

由于变量可能不存在,您可以(并且应该)确保您的代码不会触发通知:

或者,如果您想跳过手动索引检查并添加进一步的验证,您可以使用 filter 扩展:

最后但并非最不重要的是,您可以使用 null 合并运算符(自 PHP/7.0<起可用) 处理丢失的参数:

echo $_GET['link'] ??'后备价值';

I'm trying to pass a URL as a url parameter in php but when I try to get this parameter I get nothing

I'm using the following url form:

http://localhost/dispatch.php?link=www.google.com

I'm trying to get it through:

$_GET['link'];

But nothing returned. What is the problem?

解决方案

$_GET is not a function or language construct—it's just a variable (an array). Try:

<?php
echo $_GET['link'];

In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the global keyword).

Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:

<?php
if (isset($_GET['link'])) {
    echo $_GET['link'];
} else {
    // Fallback behaviour goes here
}

Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:

<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);

Last but not least, you can use the null coalescing operator (available since PHP/7.0) to handle missing parameters:

echo $_GET['link'] ?? 'Fallback value';

这篇关于PHP 中的 GET URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆