2

When checking a superglobal like $_GET or $_POST for existence of a value for a particular key, is there a method that encompasses the following functionality in a more concise way?

if (isset($_POST['foo']) && $_POST['foo'] !== '') {
    // ...
}

The isset() function by itself returns TRUE even when the value is an empty string '', so I can't use that.

Using just $_POST['foo'] !== '' will work correctly by itself, but it emits an E_NOTICE for undefined index, which is undesirable.

The empty() function returns TRUE for the value "0", which is a valid value, so I can't use that, either.

Am I missing something obvious, or is this really the best/only way to do it?

3 Answers 3

5

Probably you are looking for array_key_exists():

if (array_key_exists('foo', $_POST)){
    // will execute when $_POST['foo'] is set, even if to null, false, '' etc.
};

If you do not want to match empty string (''), then your solution is probably enough. Solution above gives you false only when the key does not exist.

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

1 Comment

I believe this will work, since $_POST values can never be NULL, FALSE, etc. (right?). I mean, barring someone overriding it, that is.
2

You can always silence the E_NOTICE. In this case, you expect a E_NOTICE to be thrown if the variable isn't set (and really, it's the only possible error condition here) so it is safe to be silenced.

if(@$_POST['foo'] != '') {

}

2 Comments

I disagree with "it's the only possible error condition here". Someone could have overwritten $_POST variable and you may get quite a broad range of errors, including eg. fatal errors: ideone.com/3DkzZ. Don't use silencing operators (stackoverflow.com/a/11588901/548696), and if someone uses it - feel free to use scream extension.
@Tadeck: On a comparison (ie.: in this specific case), no other possible error condition exists. The example you have given is not a comparison. While I agree that silencing operators should not normally be used, this is one situation where it can be used safely.
1

Why not do the whole post in one shot using array_merge at the start of the script.

i.e.

$default = array('foo' => "2", 'bar' => 'Hello World');
$postValues = array_merge($default, $POST);

Then use $postValues. It gets over isset everywhere in the rest of the code as the defaults will be used instead.

2 Comments

It took me a few minutes to understand how this would be applicable to my question. Basically, I would need to define a $default array of nothing but NULL values for all possible parameters. Then I could use the $postValues['foo'] !== '' method, since foo is guaranteed to always be set, and since NULL !== '', it would accomplish what I want.
@drrchknlsn - Even better - you could put other default values in as well.

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.