作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
PHP If 语句 if (!$_POST)
- !$_POST
是什么意思?
例如,在我正在查看的一些遗留代码中,代码如下:
<form action="paypalorders.php" method="POST">
<input type="hidden" name="orderList" value="' . $orderList . '">
<input type="submit" value="Archive">
</form>';
if (!$_POST) {
file_put_contents('/orders.txt', $output_line1);
}
我查看了很多其他线程,但没有看到提出这个确切的问题,所以我试图找出它是什么。我见过这样格式化的线程:
if(!empty($_POST)) {
但与我正在寻找的不太一样。是不是一样的东西,只是简写?我不确定,这就是我问的原因。我用 Google 搜索并查看了一些帖子,但我仍然不确定。
谢谢。
最佳答案
!
(不是)logical operator如果右侧的参数不为 true,则返回 true。它强制将参数计算为 bool 值。在这种情况下,当 $_POST
数组被评估为 bool 值时,如果它不为空,则评估为 true
;如果不为空,则评估为 false
。 (参见converting to boolean。)
if (!$_POST) {
应该是一种安全的方法来检测$_POST
中是否存在任何内容(如果您愿意)去做。在这种情况下,empty
是不必要的,因为 superglobals始终被设置,并且由于您没有引用特定的键,因此您无需担心 undefined index 通知。
我认为还值得一提的是,如果检查的唯一目的是查看发送的请求类型,那么最好只是 check the request method directly ,因为 !$_POST
not 意味着该请求不是 post,因为 post 请求可以为空。
关于PHP If 语句 if (!$_POST) - !$_POST 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51866762/
我是一名优秀的程序员,十分优秀!