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
- if($a!="" || $b!="" || $c!="")
- if($a.$b.$c!="")
- if(strlen($a) || strlen($b) || strlen($c))
- if(strlen($a)>0 || strlen($b)>0 || strlen($c)>0)
- if(strlen($a.$b.$c))
- 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.
精彩评论