1

So I am using the following style of code if(array_key_exists('some_value', $_POST)){echo 'hi';}

For PHP 5.2.17 I am getting a warning from this style of code. This is the warning: WARNING: argument 2 for array_key_exists() is not either an array or an object on line: 123

This seems strange to me because I believe that the $_POST array should always be defined. Is that not the case? I'm not sure what would cause the $_POST array to not be considered an array. I am not resetting $_POST to anything so it should exist as an array at all times. Does anyone have any idea what is wrong. Please let me know if more information is needed and thank you for the help.

Edit: I should note that this only happens on the production server. My local environment does not have this problem.

7
  • 1
    Presumably you are testing this via a browser and not via the command line? $_POST, $_GET etc are not defined by the CLI. Obvious I know, but I have to ask... Commented Apr 22, 2013 at 16:14
  • This is indeed in a browser. Commented Apr 22, 2013 at 16:20
  • 1
    Are you using any frameworks? Notably, is it possible that anything has called unset($_GET); anywhere? Unsetting a superglobal in any scope propagates to all scopes. What do you get if you var_dump($_GET); ? Commented Apr 22, 2013 at 16:26
  • I'm running wordpress. Could wordpress be unsetting this? Commented Apr 22, 2013 at 16:27
  • 1
    Well it comes down to this: If it's null, something probably unset() it somewhere. If it's anything else, something reassigned it somewhere. Realisitically those are the two options. Commented Apr 22, 2013 at 16:39

2 Answers 2

2

The Superglobals $_POST and $_GET are only populated if the script is POSTed to or GET from. In your example, the reason that you'd get that error is if there was not post action to the script. Before checking for a certain post value, you should check to make sure there was a post:

if(isset($_POST)) {
    //The form was posted
}

In that fashion. From there, you can check for certain values using array_key_exist, or you can further check isset($_POST['myKey']).

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

Comments

2

Use if(isset($_POST['some_value'])) { echo 'hi'; } instead. Never had a problem with it.

Also check if you are not overriding or unsetting $_POST (or some framework you are using is doing it for you). I avoid to do so with superglobal variables since I think it is a bad practice and might give headaches like this one.

8 Comments

It's true that this will solve the issue - although it's still a little odd that this specific error should occur, $_POST should always be an array in a web server SAPI
Its true, I'm wondering why $_POST is not defined really, it just seems so strange.
not truly reliable. $arr['x'] = null. array_key_exists() will report TRUE, but isset() will report false.
I guess both ways should work but I always use isset and never had a $_POST not defined problem. this is strange
the only reason it occurs me is that you override it. I avoid to override $_POST and similar variables since I think it is a really bad practice
|

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.