$_GET 和 isset()
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有时我使用全局
GET
函数:Sometimes I use a global
GET
function:所有这些混乱的主要思想很简单:程序中的每个变量都应该在使用前初始化。
因此,您能做的最好的事情就是将所有变量集中设置,例如在脚本的最顶部。
但也有一些不同的情况。对于您发布的案例,您不需要这些,但是
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
已经回答了,但由于没有提到,也请尝试:
如果不存在,则返回 null,没有烦人的数组通知,并且还以各种方式过滤掉潜在的坏东西。另请参阅:http://www.php.net/filter
Already answered, but since it wasn't mentioned, also try:
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
您可以创建一个函数来为您执行此操作。
然后你可以执行 if(getVar('whatever') == "whatever")。
You can make a function to do this for you.
Then you can do
if(getVar('whatever') == "whatever")
.这里有两种方法。
我能想到的第三个选择是将 $_GET 放入它自己的类中。 PHP 重载是实现此目的的便捷技巧。
http://php.net/manual/en/language.oop5.overloading。 php
我不想写一个例子来证明这一点。 :)
Here are two methods.
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. :)
简单我认为你想检查 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
无通知
$key = &$_POST['key'];
这个方式(函数内部)它已被破坏,并显示错误:
no notice
$key = &$_POST['key'];
This way (inside function) it is depracted, and shows error:
在编写此类脚本时,我通常在顶部执行类似的操作:
然后,在测试值时,它看起来像这样:
它似乎对我来说工作得很好。
When writing these sorts of scripts, I usually do something like this at the top:
Then, when testing the values, it looks something like this:
It seems to work pretty well for me.