php - check if $_POST is array?

前端 未结 8 608
独厮守ぢ
独厮守ぢ 2020-12-21 14:58

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.

相关标签:
8条回答
  • 2020-12-21 15:07

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

    0 讨论(0)
  • 2020-12-21 15:08

    If it hasn't been changed in some manner like

    $_POST = 'not array';
    

    then it is array ;-)

    0 讨论(0)
  • 2020-12-21 15:13

    That check is unnecessary. $_POST is a superglobal array which is always defined. You should just check for specific elements using isset

    0 讨论(0)
  • 2020-12-21 15:16

    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

    0 讨论(0)
  • 2020-12-21 15:17
    • $_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
    0 讨论(0)
  • 2020-12-21 15:17

    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.

    0 讨论(0)
提交回复
热议问题