PHP if($ _ POST)与if(isset($ _ POST)) [英] Php if($_POST) vs if(isset($_POST))

查看:558
本文介绍了PHP if($ _ POST)与if(isset($ _ POST))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格,如下所示:

I have a simple form as demonstrated below:

<form action="" method="post">
  <input type="text" />
  <input type="submit" value="SEND" />
</form>

当我尝试通过if($_POST)接收从此表单发送的数据时,我失败了,但是当尝试isset时,我成功了.

When I try to receive data sent from this form via if($_POST), I fail, but when try with isset, I success.

if($_POST){
  echo 'a'; //Doesn't print anything.
}
if(isset($_POST)){
  echo 'b'; //Prints 'b'
}

我猜想它背后的原因是我的表单输入中缺少名称属性,但是我不明白为什么if($_POST)isset($_POST)在这种情况下会有不同的反应方式.

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.

推荐答案

isset 确定是否设置了变量并且该变量不是NULL . $_POST将始终被设置并且将始终是一个数组.

isset determine if a variable is set and is not NULL. $_POST will always be set and will always be an array.

没有isset,您只是在测试该值是否真实.空数组(如果您不发布任何数据,则为$_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.

这篇关于PHP if($ _ POST)与if(isset($ _ POST))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆