php - 检查 $_POST 是否是数组?

发布于 2024-10-31 04:42:47 字数 145 浏览 6 评论 0原文

我知道这可能是一个愚蠢的问题,但我遇到了一段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

征棹 2024-11-07 04:42:47

如果它没有以某种方式改变

$_POST = 'not array';

,那么它就是数组;-)

If it hasn't been changed in some manner like

$_POST = 'not array';

then it is array ;-)

童话里做英雄 2024-11-07 04:42:47

$_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

云胡 2024-11-07 04:42:47
  • $_POST 是一个超全局变量,并且始终定义(始终存在)并且始终是一个数组,
  • 这是事实,即使它不包含任何元素
  • 也是可能的,如果不建议并且我从未见过它,要覆盖或取消设置它,
  • 您不需要 $_POST 数组的 isset() 和 is_array() ,但您经常需要它们来表示 $_POST 数组中的元素
  • $_POST is a superglobal and is always defined (always exists) and is always an array
  • this is true, even if it doesn't contain any elements
  • it is possible though, if not advisable and I've never seen it, to overwrite or unset it
  • you don't need isset() and is_array() for the $_POST array but you will quite often need them for elements in the $_POST array
も星光 2024-11-07 04:42:47

那个检查是不必要的。 $_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

娇女薄笑 2024-11-07 04:42:47

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.

独﹏钓一江月 2024-11-07 04:42:47

正如许多人已经说过的那样,它始终是一个数组。

我认为目的可能是检查空数组。 !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

弥枳 2024-11-07 04:42:47

$_POST 始终被定义为数组,即使它不包含任何键/值对。

$_POST is always defined as an array even it doesn't contain any key/value pairs.

笑,眼淚并存 2024-11-07 04:42:47

正如已经多次提到的,$_POST 是一个超全局变量,它总是被定义并且总是一个数组(除非被覆盖)。

如果您尝试测试是否已发布某些内容,则可以使用如下所示的内容:

if (count($_POST)) {
    // something has been submitted
}

要回答主要问题,不,不需要 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:

if (count($_POST)) {
    // something has been submitted
}

To answer the main question, no, the is_array check is not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文