PHP if($_POST) vs if(isset($_POST))
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34609086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php if($_POST) vs if(isset($_POST))
提问by Aycan Ya??t
I have a simple form as demonstrated below:
我有一个简单的表格,如下所示:
<form action="" method="post">
<input type="text" />
<input type="submit" value="SEND" />
</form>
When I try to receive data sent from this form via if($_POST)
, I fail, but when try with isset
, I success.
当我尝试通过 接收从此表单发送的数据时if($_POST)
,我失败了,但是当尝试使用 时isset
,我成功了。
if($_POST){
echo 'a'; //Doesn't print anything.
}
if(isset($_POST)){
echo 'b'; //Prints 'b'
}
I guess the reason behind it is missing name attribute in my form input, but I can't understand why if($_POST)
and isset($_POST)
react different ways in this case.
我想背后的原因是缺少name属性在我的形式输入,但我不明白为什么if($_POST)
和isset($_POST)
在这种情况下的反应不同的方式。
回答by Quentin
isset
determine if a variable is set and is not NULL. $_POST
will always be set and will always be an array.
isset
确定变量是否已设置并且不是 NULL。$_POST
将始终被设置,并将始终是一个数组。
Without isset
you are just testing if the value is truthy. An empty array (which $_POST
will be if you aren't posting any data) will not be truthy.
如果没有,isset
您只是在测试该值是否为真。空数组($_POST
如果您不发布任何数据,则为空数组)不会是真实的。
回答by Daniele D
isset determines if a variable is set and not NULL, see the manual: http://php.net/manual/en/function.isset.php
isset 确定是否设置了变量而不是 NULL,请参阅手册:http: //php.net/manual/en/function.isset.php
while if($_POST)
checks $_POST
for being true.
whileif($_POST)
检查是否$_POST
为真。
in your case, $_POST
will always be set. If doing that with other variables not related to a form, keep in mind that checking for if($var)
without knowing if it is set or not, will throw a notice. Checking if(isset($var))
will not throw a notice.
在您的情况下,$_POST
将始终设置。如果使用与表单无关的其他变量执行此操作,请记住,在if($var)
不知道是否已设置的情况下进行检查会抛出通知。检查if(isset($var))
不会抛出通知。
Unrelated to your question: if you want to know if there is data inside your $_POST
array you could try working with count($_POST)
, see:
http://php.net/manual/en/function.count.php
与您的问题无关:如果您想知道您的$_POST
数组中是否有数据,您可以尝试使用count($_POST)
,请参阅:http:
//php.net/manual/en/function.count.php
回答by n-dru
It is because the $_POST
is an array of inputs names/values pairs, and in your form no input has any name, therefore it is an empty array (evaluating to false). You can verify it by var_dump($_POST)
.
这是因为这$_POST
是一个输入名称/值对的数组,并且在您的表单中没有输入具有任何名称,因此它是一个空数组(计算为 false)。您可以通过 验证它var_dump($_POST)
。
Try to add a name to text input to access its value:
尝试向文本输入添加名称以访问其值:
<form action="" method="post">
<input type="text" name="somename" />
<input type="submit" value="SEND" />
</form>
回答by Akash Tyagi
The major difference is issetdetermine variable is set and is not null for $_POSTis not work here because you are not define here input name. The $_POSTconsider an array of inputs name/values pairs.
主要区别是isset确定变量已设置并且不为空,因为$_POST在这里不起作用,因为您没有在此处定义输入名称。该$ _ POST考虑输入名称/值对的数组。