似乎不起作用: if($_SERVER['REQUEST_METHOD'] == 'GET') 用于 GET 请求?
无论是否传递 GET 变量,以下代码都会执行:
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
//Do something
}
以下代码仅在传递 GET 变量时执行:
if($_GET)
{
//Do something
}
我的印象是第一种方法更好,但现在我很困惑。
有什么想法吗?谢谢!
The following code executes whether there are GET variables passed or not:
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
//Do something
}
The following only executes when GET variables are passed:
if($_GET)
{
//Do something
}
I was under the impression that the first method was better, but now I am confused.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个代码将在请求方法为 GET 时执行,即使不存在查询字符串。
它不会以 POST 请求类型执行,即使存在查询字符串。
您必须明白“GET”请求类型并不意味着在 URL 中传递了变量。
因此这两个代码是为完全不同的任务而编写的。
如果您只是需要检查如果变量在 URL 中传递,则使用第二个。
The first code will execute when the request method is GET, even if no query string is present.
It won't be executed with a POST request type, even if there is a query string.
You have to understand that the 'GET' request type does not mean that variables were passed in the URL.
So the two codes are made for completely different tasks.
If you simply need to check if variables were passed in the URL, use the second one.