简体   繁体   中英

checking if a array value is not empty without php warnings

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?

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.

Using zf, use this

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

Best practice IMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM