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

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

问题描述

我正在尝试将URL作为php中的url参数传递,但是当我尝试获取此参数时却一无所获

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

我正试图通过它:

$_GET['link'];

但是什么也没返回.有什么问题吗?

But nothing returned. What is the problem?

推荐答案

$_GET不是函数或语言构造,而只是一个变量(数组).试试:

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

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

尤其是 superglobal :在由PHP填充并在所有范围内都可用的变量中(您可以在没有全局关键字).

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
}

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

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);

最后但并非最不重要的是,您可以使用 PHP/7.0 )来处理缺少的参数:

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天全站免登陆