php isset 您在表单检查中需要它吗?
我试图了解在表单处理过程中是否需要 isset,当我检查 $_REQUEST["input_name"] 时,如果没有传递任何值,它不会对此哭泣,并且如果您尝试访问 a php 不会抛出错误不存在的数组项...我可以使用 if($_REQUEST["input_name"]).. 即使在这些情况下,“空”又如何,我可以使用 if()
THnks
I trying to understand if a isset is required during form processing when i check $_REQUEST["input_name"] if no value is passed it doesn't cry about it and php doesn't throw out an error if you are trying to access a array item which doesn't exist....i can use if($_REQUEST["input_name"])..
what about "empty" even in those cases i can use if()
THnks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有不同类型的错误级别。 检查未设置的变量只会引发通知。 您的错误报告可能设置为忽略这些。 当您想要检查变量是否已设置时,最佳实践是始终使用
isset
,尽管 它确实有它的陷阱。仅执行上面所做的操作,例如,如果
$_REQUEST["input_name"]
是字符串“0”,则它将计算为 false。 另外,一开始就使用 $_REQUEST 也不是一个好主意,因为它可能会受到 cookie 等内容的影响,而且通常是不良架构的代码味道。There are different type of error levels. Checking a variable that is not set only throws a notice. Your error reporting is probably set to ignore those. It is best practice to always use
isset
when you want to check if a variable has been set, although it does have its gotchas.Doing only what you are doing above, for example, if
$_REQUEST["input_name"]
is the string "0", it will evaluate to false. Also it is not a good idea to use $_REQUEST to begin with, as it can be affected by stuff like cookies and such and it's usually a code smell for bad architecture.我不建议使用
$_REQUEST
超全局来捕获表单输入,除非您正在测试表单。 请改用$_GET
或$_POST
,除非您有充分的理由。此外,isset() 和 array_key_exists() 都对数组键执行相同的技巧,尽管 array_key_exists() 在数组中更清晰语境。
我建议
在您的开发环境中使用:,因为这可以暴露可以应用更好的实践的地方,例如在使用变量之前未能声明变量等。
I wouldn't recommend using the
$_REQUEST
superglobal for capturing form input, unless you're testing a form. Use$_GET
or$_POST
instead, unless you have a really good reason.Also,
isset()
andarray_key_exists()
both do the same trick with regard to array keys, althougharray_key_exists()
is clearer in an arrays context.I recommend using:
within your development environment, as that can expose where better practices might be applied, such failure to declare variables before they are used, etc.
使用 $_REQUEST 几乎是一种黑客行为。 您应该使用 $_POST 或 $_GET (取决于您在做什么)并且您应该使用 isset()。
我读过的所有关于 PHP 的书似乎都这么说。
using $_REQUEST is pretty much a hack. You should be using $_POST or $_GET (depending on what you are doing) and you should use isset().
Every book I've read on PHP seems to say that.
一般来说,至少对于测试来说,在 php.ini 中或使用 error_reporting(E_ALL); 在代码中将错误报告设置为 E_ALL(所有错误和警告); (也考虑添加 E_STRICT。)预先获得有关错误的明显通知,而不是出现一些微妙的错误,直到稍后才发现。
避免使用 $_REQUEST,它太模糊了(它包括 GET、POST 和 cookie 值),如果这些是您真正的意思,请使用 $_POST 或 $_GET,并使用
isset($_POST["input_name "])
简短的回答是“是”。 :)
Generally, at least for testing, set error reporting to E_ALL (all errors and warnings) either in your php.ini or in code using error_reporting(E_ALL); (Look into adding E_STRICT too.) Better to get an obvious notice about an error up front, than to have something subtle go wrong that you don't catch till later.
Avoid using $_REQUEST, which is too vague (it includes GET, POST AND cookie values), and use the $_POST or $_GET if those are what you really mean, and do check with
isset($_POST["input_name"])
The short answer is "Yes." :)
如果“input_name”不存在,则会抛出通知(错误),因此建议使用isset()。
will throw a notice (error) if "input_name" doesn't exist, so isset() is recommended.