1

I'm using Zend_Form subclasses to pass array of data to my controllers and sometimes I need some extra logic in the controllers, as such:

$post = $request->getPost();
if (array_key_exists('signatureData', $post) && !empty($post['signatureData'])) {
   ...
}

To avoid getting php warnings, first, I need to check if the signatureData key exists and only then I can check if the value is not empty.

Anyway I can make this IF statement a little shorter without adding custom php function?

2 Answers 2

6

Accepted standard :

if (isset($post['signatureData']) && !empty($post['signatureData']))

Apparently, this is also possible :

if (!empty($post['signatureData']))

-edit-

If you're really lazy, there's a dirty way :

if (@$post['signatureData'])

I'd recommend against it though.

As pointed out in the comments, don't actually use this. It's bad, bad, bad bad, bad. Really though, please don't ever use it. It's only here as a reference.

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

6 Comments

isnt an empty check enough? if the key doesnt exist, an empty check will return true, and if the key does exist but contains no value, empty will also return true.
@Julien - It is not enough, as it will raise a notice of an undefined index if $post['signatureData'] is not set. I would also remove "the dirty way" as a possibility, as its a poor technique to create a habit out of.
It will also give a Notice: warning about checking an undefined index.
if the key doesn't exist, an empty check will return true and NOT raise a notice. From the PHP docs: "empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."
Hmm, interesting - didn't know that, and I've never seen it used like that in code. Will update my answer :-)
|
1

Using zf, use this

If ($request->getParam('signatureData', false))

Best practice IMO

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.