2

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?

1
  • Not always. I'm personally using an object wrapper around $_POST, $_GET and Co. They work as array still, but are ArrayObjects and would fail that simple is_array() check. But that's an awfully special case. Not usually a concern. Commented Apr 8, 2011 at 23:06

8 Answers 8

7

If it hasn't been changed in some manner like

$_POST = 'not array';

then it is array ;-)

Sign up to request clarification or add additional context in comments.

3 Comments

Exactly. Unless otherwise explicitly declared the $_POST superglobal is always an array.
ok, but, it can be changed only by the coder or it can be setted to string also by form submitters? can this be a security issue?
@Julie Rokk: if it hasn't been changed explicitly by a developer as I answered - it is always an array. It is not possible to change it outside.
3

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

Comments

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

1 Comment

thank's for your responce, i want to up-vote your answer, cause it is detailed, although i feel to check the zerkms one, cause it said what i was looking for; i already knew all the other details.
2

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

Comments

1

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.

Comments

1

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.