$_GET 和 isset()

发布于 2024-09-17 19:23:13 字数 722 浏览 5 评论 0原文

在 if 语句的其余部分之前,我正在尝试 if isset($_GET['whatever'])... 。 E_NOTICE 错误是一种方便关闭的方法,对于 $_POST 变量,我在我的初始化脚本中有一个解决方案

$POST = (is_array( $_POST ) && count( $_POST ) > 0);

我发现这有助于自我发布脚本看起来干净。

if ($POST) {
    // now do something with $_POST...
}

如果您不知道关键是什么,我不确定如何动态执行此操作?任何人都可以帮助找到 $_GET 变量的类似解决方案吗?

编辑:

我只是希望 if ($_GET['whatever'] == "whatever"); 在未设置且没有 E_NOTICE 的情况下返回 false代码>错误。

编辑:

抱歉,如果我不清楚我正在寻找 $_GET 而不是 $_POST 的解决方案 - 我只使用 $_POST 作为我希望实现的目标的示例。

I am getting tried of if isset($_GET['whatever'])... before the rest of my if statement. E_NOTICE errors are way to handy to turn off and for $_POST variables I have a solution in my init script..

$POST = (is_array( $_POST ) && count( $_POST ) > 0);

I find this helps self posting scripts look clean.

if ($POST) {
    // now do something with $_POST...
}

I'm not sure how to dynamically do this if you have no idea what the key is? Can anyone help find a similar solution for $_GET variables?

EDIT:

I simply want if ($_GET['whatever'] == "whatever"); to return false if it's not set and no E_NOTICE errors.

EDIT:

Sorry if I'm unclear I'm looking for a solution for $_GET not $_POST--I only am using $_POST as an example of what I hope to achieve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

月下客 2024-09-24 19:23:13

有时我使用全局 GET 函数:

function GET($key, $default = null) {
    return isset($_GET[$key]) ? $_GET[$key] : $default;
}

Sometimes I use a global GET function:

function GET($key, $default = null) {
    return isset($_GET[$key]) ? $_GET[$key] : $default;
}
ヅ她的身影、若隐若现 2024-09-24 19:23:13

所有这些混乱的主要思想很简单:程序中的每个变量都应该在使用前初始化。
因此,您能做的最好的事情就是将所有变量集中设置,例如在脚本的最顶部。

但也有一些不同的情况。对于您发布的案例,您不需要这些,但是

if ($_SERVER['REQUEST_METHOD'] == "POST")

The main idea of all that mess is simple: every variable in your program should be initialized before use.
Thus, the best you can do is to set all variables centralized, say, at the very top of your script.

But there are some different cases. For the case you posted, you need none of these but

if ($_SERVER['REQUEST_METHOD'] == "POST")
堇色安年 2024-09-24 19:23:13

已经回答了,但由于没有提到,也请尝试:

$var = filter_input(INPUT_GET, 'varname');

如果不存在,则返回 null,没有烦人的数组通知,并且还以各种方式过滤掉潜在的坏东西。另请参阅:http://www.php.net/filter

Already answered, but since it wasn't mentioned, also try:

$var = filter_input(INPUT_GET, 'varname');

That will return null if it doesn't exist, without the annoying array notices, and also filters out potential bad stuff in various ways. See also: http://www.php.net/filter

夏日浅笑〃 2024-09-24 19:23:13

您可以创建一个函数来为您执行此操作。

function getVar($item){
    return isset($_GET[$item]) ? $_GET[$item] : '';
}

然后你可以执行 if(getVar('whatever') == "whatever")。

You can make a function to do this for you.

function getVar($item){
    return isset($_GET[$item]) ? $_GET[$item] : '';
}

Then you can do if(getVar('whatever') == "whatever").

橘味果▽酱 2024-09-24 19:23:13

这里有两种方法。

/*
* Makes any request keys into variables of the same name
*/
foreach($_GET AS $key => $value) {
    ${$key} = ($value);
}    

//Assuming a input key of $_GET['whatever'] 
echo $whatever;

/*
* Casting to an object
*/
$get = (object) $_GET;

//Assuming a input key of $_GET['whatever'] 
echo $get->whatever

我能想到的第三个选择是将 $_GET 放入它自己的类中。 PHP 重载是实现此目的的便捷技巧。

http://php.net/manual/en/language.oop5.overloading。 php

我不想写一个例子来证明这一点。 :)

Here are two methods.

/*
* Makes any request keys into variables of the same name
*/
foreach($_GET AS $key => $value) {
    ${$key} = ($value);
}    

//Assuming a input key of $_GET['whatever'] 
echo $whatever;

/*
* Casting to an object
*/
$get = (object) $_GET;

//Assuming a input key of $_GET['whatever'] 
echo $get->whatever

A third option that I can think of is making the $_GET into its own class. PHP overloading is handy trick for doing this.

http://php.net/manual/en/language.oop5.overloading.php

I didn't feel like writing up an example demonstrating this. :)

再见回来 2024-09-24 19:23:13

简单我认为你想检查 get value 是否设置然后需要执行下一步否则做不同的事情

我喜欢第一个答案但需要检查它是否适合你

if(isset($_GET['whatever']){
if ($_GET['随便'] == "随便");{
//做剩下的事情
}
别的{
//做剩下的事情
}
帮助

如果我认为你需要的话,希望它能有所

simple I think you want to check if get value is set then need to do the next step otherwise do different

I like the first answer but need to check thats if it works for you

if(isset($_GET['whatever']){
if ($_GET['whatever'] == "whatever");{
//do the rest
}
else{
//do the rest
}
}

hope it may help if what I think is your need

入怼 2024-09-24 19:23:13
  • 简短
  • 无通知

    $key = &$_POST['key'];

这个方式(函数内部)它已被破坏,并显示错误:

function f($s){}
f(&$_POST['key']);

致命错误:调用时传递引用已被删除

  • short
  • no notice

    $key = &$_POST['key'];

This way (inside function) it is depracted, and shows error:

function f($s){}
f(&$_POST['key']);

Fatal error: Call-time pass-by-reference has been removed

隔岸观火 2024-09-24 19:23:13

在编写此类脚本时,我通常在顶部执行类似的操作:

$submitted = isset($_POST['submit']) ? true : false;
$username = isset($_POST['username']) ? $_POST['username'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
(etc.)

然后,在测试值时,它看起来像这样:

if ($submitted)
{
    if ($username && $password)
    {
        // do stuff with username and password including testing their values
    }
}

它似乎对我来说工作得很好。

When writing these sorts of scripts, I usually do something like this at the top:

$submitted = isset($_POST['submit']) ? true : false;
$username = isset($_POST['username']) ? $_POST['username'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
(etc.)

Then, when testing the values, it looks something like this:

if ($submitted)
{
    if ($username && $password)
    {
        // do stuff with username and password including testing their values
    }
}

It seems to work pretty well for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文