开发者

Optimal way of checking if any of multiple strings are non-empty (in php)

I have several strings which may or may not be empty. If any of them are non-empty, a mysql insertion should be called.

I've coded it simply, but was thinking about the fastest method of testing if ANY of them is non-empty.

$a = something;
开发者_Go百科$b = something;
$c = something;

Options

  1. if($a!="" || $b!="" || $c!="")
  2. if($a.$b.$c!="")
  3. if(strlen($a) || strlen($b) || strlen($c))
  4. if(strlen($a)>0 || strlen($b)>0 || strlen($c)>0)
  5. if(strlen($a.$b.$c))
  6. if(strlen($a.$b.$c)>0)


I would do something like this:

if (!$a || !$b || !$c){
   ...
}

There is no overhead of a function like strlen etc. The ! is good bet there.


Wouldn't one way of finding out be testing by generating, let's say, at set of 1000 random strings and traverse that set for each option? I assume it's quite a naive way and reading about the native code for each function would generate a more thorough answer, but still, it's easy :)


You forgot the most relevant functions on your list, namely empty and isset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜