php - @$_POST 和 $_POST 有什么区别....?

标签 php html forms post

<分区>

我正在寻找在用户刷新浏览器后将用户输入保存在表单中的不同方法,我遇到了这个:

value="<?php echo @$_POST['blah'];?>">

我也看到了像下面这样的:

value="<?php echo $_POST['blah'];?>">

他们似乎都在做同样的事情。 $_POST 数组前面的 @ 符号有什么意义?一种方式优于另一种方式吗?

最佳答案

@ 是 PHP 的 error control operator .基本上,将它放在表达式(例如数组访问或函数调用)之前将抑制通常会生成的任何错误。在功能上,没有区别,只是根据您的设置会生成、记录或显示哪些警告/通知。

在这种情况下,如果 'blah' 未在 $_POST 数组中定义,则第一种形式(带有 @)不会生成通知,而第二种形式会。

至于什么是首选,根据我的经验,通常不鼓励使用 @,就像任何只会抑制错误的东西一样 - 更好的做法是先主动检查事情。除其他原因外,PHP 文档还给出了以下理由来警惕它:

Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

TL;DR:它会抑制错误,通常不鼓励这样做,除非您有充分的理由这样做。最好积极主动。

关于php - @$_POST 和 $_POST 有什么区别....?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23260545/

相关文章:

php - 逗号分隔具有一个值的步骤到具有相同值的多个步骤

javascript - 视口(viewport)问题/无滚动

html - 将背景位置与图像 Sprite 的百分比一起使用

javascript - 为什么 Facebook 导航栏固定并放置在顶部不会在移动到其他页面时重新加载?

jquery - 出现 "Object #<NodeList> has no method ' click'"错误?

php - 你如何使用 Javascript 来复制表单域?

php - 处理 PHP PDO postgres 中的 bool 输入?

php - preg_replace 度数和 phi 符号

javascript - 提交处理程序仅在第二次尝试时起作用

html - 如何通过更多的一键按钮将焦点设置在表单上?