!empty($ _ POST)与$ _SERVER ['REQUEST_METHOD'] =='POST' [英] !empty($_POST) vs. $_SERVER['REQUEST_METHOD'] == 'POST'
问题描述
如果发现此问题 $ _POST与$ _SERVER ['REQUEST_METHOD'] =='POST ,人们似乎更喜欢选择$_SERVER['REQUEST_METHOD'] == 'POST'
,而不是选择$_POST
或isset($_POST)
.
If found this question $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' and people seems to prefer check $_SERVER['REQUEST_METHOD'] == 'POST'
rather than check $_POST
or isset($_POST)
.
(已经,我不明白为什么人们总是检查isset($_POST)
,而总是将其设置)
(Already, i don't understand why people check isset($_POST)
, while it is always set)
但是,如果我们检查!empty($_POST)
而不是全部检查呢?
But what if we check !empty($_POST)
instead all of this ?
我的意思是,$_POST
超全局变量始终在页面上定义为空的array
,因此仅检查它是否不为空就意味着已提交具有post method
的表单,我错了吗?如果不为空,我们可以检查我们想要在其中的每个字段.
I mean, the $_POST
superglobale is always defined on a page as an empty array
, so just checking if it's not empty mean thats a form with the post method
has been submitted, am I wrong ? If it's not empty, we can check each fields we want inside.
我不明白为什么我们需要通过选中
I don't get why we need to add an additional step by checking
$_SERVER['REQUEST_METHOD'] == 'POST'
(而且,三重"="会更好吗?)
(Also, triple "=" wouldn't be better ?)
我进行了搜索,但未找到任何内容.如果我什么也没找到,那可能意味着我在说废话^^.
I've searched but found nothing about it. And if i find nothing, it probably means that i'm saying nonsense ^^.
有人可以告诉我我是否错了吗?
Can someone tell me if I am wrong ?
推荐答案
PHP选择像这样命名$_POST
和$_GET
的选择是很糟糕的,并且在这里最容易引起混淆. $_GET
包含请求URL的查询参数,而$_POST
包含已解析的请求主体数据(如果它是application/x-www-form-urlencoded
).
PHP's choice to name $_POST
and $_GET
as they did was terrible, and is mostly leading to confusion here. $_GET
contains the request URL's query parameters, while $_POST
contains the parsed request body data (if it's application/x-www-form-urlencoded
).
任何URL以及任何请求都可以包含URL查询参数,因此$_GET
可以并且可以用于任何类型的请求,而不仅仅是HTTP GET
请求. HTTP POST
和 PUT
请求(以及可能由您组成的其他任何动词)可以包含urlencoded
请求主体,该主体可能会/可能最终出现在$_POST
中(不确定我最想知道的是PHP是否会麻烦解析主体数据(除了POST
请求之外的任何内容). POST
或PUT
请求也不必包含正文数据.如果空的POST
请求在您的应用程序中有意义,那么就这样.
Any URL, and thereby any request, can contain URL query parameters, so $_GET
can be and is used with any sort of request, not just HTTP GET
requests. HTTP POST
and PUT
requests (and arguably any other verb you make up) can contain a urlencoded
request body, which would/could end up in $_POST
(not sure off the top of my head whether PHP bothers parsing body data for anything but POST
requests). A POST
or PUT
request also doesn't have to contain body data; if an empty POST
request makes sense in your application, so be it.
因此,$_POST
,POST
,$_GET
,GET
,请求正文数据,查询参数等都非常正交.
So, $_POST
, POST
, $_GET
, GET
, request body data, query parameters etc. are all rather orthogonal to each other.
如果要检查HTTP请求的方法,请检查$_SERVER['REQUEST_METHOD']
.如果您想知道是否发送了任何Urlencoded正文数据,请检查$_POST
中是否有数据.为此,if ($_POST)
就足够了. 空数组的值为 falsey .通常,您可以用一个替换另一个,但这取决于您要检查的内容.
If you want to check the method of the HTTP request, check $_SERVER['REQUEST_METHOD']
. If you want to know whether any urlencoded body data was sent, check whether there's data in $_POST
. For that purpose, if ($_POST)
is enough. An empty array evaluates as falsey. Oftentimes you can probably substitute one for the other, but that depends on what you're trying to check.
empty
用于禁止错误报告,否则该变量未定义.除此之外,它的行为与普通的 truthy/falsey 检查一样.
empty
is used for suppressing error reporting on otherwise undefined variables. Other than that it behaves exactly as a normal truthy/falsey check.
这篇关于!empty($ _ POST)与$ _SERVER ['REQUEST_METHOD'] =='POST'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!