php - 检查 $_POST 是否是数组?
我知道这可能是一个愚蠢的问题,但我遇到了一段 php 代码片段,它在执行其他函数之前检查 $_POST
is_array()
是否。
现在我想 $_POST 应该始终是关联数组吗?真的需要这项检查吗?为什么?
I know this may be a silly question, but I come across a snippet of php code that check if the $_POST
is_array()
before execute other functions.
Now I guess that $_POST should be always an associative array or not? is this check really needed? and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果它没有以某种方式改变
,那么它就是数组;-)
If it hasn't been changed in some manner like
then it is array ;-)
$_POST 始终是一个数组,他们可能会检查某个 $_POST 值是否是一个数组。
$_POST['test'] 不是数组
$ _POST['test'] 是一个数组
$_POST is always an array, they're probably checking if a certain $_POST value is an array.
<input name="test" />
$_POST['test'] is not an array<input name="test[]" />
$_POST['test'] is an array那个检查是不必要的。 $_POST 是一个总是被定义的 superglobal 数组。您应该只使用
isset
检查特定元素That check is unnecessary. $_POST is a superglobal array which is always defined. You should just check for specific elements using
isset
PHP 确保 $_POST 始终是一个数组,您不需要执行该检查,除非您在代码中的某个位置以某种方式取消设置或覆盖 $_POST 。
PHP makes sure that $_POST is always an array, you don't need to do that check unless somewhere in your code you either unset or overwrite $_POST somehow.
正如许多人已经说过的那样,它始终是一个数组。
我认为目的可能是检查空数组。 !empty($_POST) 应该就可以了。
也许编码器有一些部分将数组更改为字符串(如果你问我的话,这很愚蠢)并想要进行检查,否则如果该语句首先出现,那么它是不必要的
Its always an array as many already gave said.
I think the intention is maybe to check for an empty array. !empty($_POST) should do just fine.
Maybe the coder has sections where the array is changed to a string (dumb if you ask me) and wants to make the check, else if that statement comes first, then its unnecessary
$_POST 始终被定义为数组,即使它不包含任何键/值对。
$_POST is always defined as an array even it doesn't contain any key/value pairs.
正如已经多次提到的,
$_POST
是一个超全局变量,它总是被定义并且总是一个数组(除非被覆盖)。如果您尝试测试是否已发布某些内容,则可以使用如下所示的内容:
要回答主要问题,不,不需要
is_array
检查。As already mentioned several times,
$_POST
is a superglobal that is always defined and always an array (unless overwritten).If you're attempting to test if something has been posted, you could use something like follows:
To answer the main question, no, the
is_array
check is not required.