简体   繁体   English

在没有php警告的情况下检查数组值是否不为空

[英]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: 我正在使用Zend_Form子类将数据数组传递给控制器​​,有时我需要在控制器中添加一些额外的逻辑,例如:

$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. 为了避免收到php警告,首先,我需要检查signatureData密钥是否存在,然后才能检查该值是否为空。

Anyway I can make this IF statement a little shorter without adding custom php function? 无论如何,我可以使该IF语句更短而无需添加自定义php函数?

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 使用zf,使用它

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

Best practice IMO IMO最佳做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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