Reputation: 560
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?
Upvotes: 2
Views: 8768
Reputation: 21989
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.
Upvotes: 0
Reputation: 2805
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
Upvotes: 1
Reputation: 40685
Upvotes: 3
Reputation: 102824
$_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
Upvotes: 3
Reputation: 3982
$_POST is always defined as an array even it doesn't contain any key/value pairs.
Upvotes: 0
Reputation: 34234
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.
Upvotes: 1
Reputation: 4816
That check is unnecessary. $_POST is a superglobal array which is always defined. You should just check for specific elements using isset
Upvotes: 2
Reputation: 255015
If it hasn't been changed in some manner like
$_POST = 'not array';
then it is array ;-)
Upvotes: 7