似乎不起作用: if($_SERVER['REQUEST_METHOD'] == 'GET') 用于 GET 请求?

发布于 2024-08-31 09:22:20 字数 255 浏览 1 评论 0原文

无论是否传递 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 技术交流群。

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

发布评论

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

评论(1

挽你眉间 2024-09-07 09:22:20

第一个代码将在请求方法为 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.

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