测试一个数组一个空字符串,并返回一个int(0或1) - 最佳实践 [英] Testing an array for an empty string and returning an int (0 or 1) - best practice

查看:115
本文介绍了测试一个数组一个空字符串,并返回一个int(0或1) - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用什么:

foreach($a as $b){if($b=='')return 0;}return 1;  // works but verbose

我的第二次​​尝试:

My second attempt:

return (in_array('',$a)+0); // returns the opposite of what I need

我目前使用的是什么:

What I am currently using:

return in_array('',$a)?'0':'1';

使用每JRL铸造(最短)

using a cast per JRL (shortest)

return (int)!in_array('',$a);

我想这是做到这一点的最好办法,但只是想验证。这是测试数组中的空值的最佳方式?

I assume this is the best way to do this, but just wanted to validate. Is this the best way to test for an empty value in an array?

我知道这化验0,NULL,FALSE或空字符串,这是O.K。

I am aware that this tests for 0,NULL,FALSE or an empty string and that is O.K.

推荐答案

其他替代方案:

return (int)!in_array('', $a));

这篇关于测试一个数组一个空字符串,并返回一个int(0或1) - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆