简体   繁体   English

检查多个字符串是否为非空的最佳方式(在php中)

[英]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. 如果其中任何一个非空,则应调用mysql插入。

I've coded it simply, but was thinking about the fastest method of testing if ANY of them is non-empty. 我已经对其进行了简单的编码,但是正在考虑最快的测试方法是否其中的任何一个都是非空的。

$a = something;
$b = something;
$c = something;

Options 选项

  1. if($a!="" || $b!="" || $c!="") if($ a!=“” || $ b!=“” || $ c!=“”)
  2. if($a.$b.$c!="") 如果($ A $ B,$ C!= “”)
  3. if(strlen($a) || strlen($b) || strlen($c)) if(strlen($ a)|| strlen($ b)|| strlen($ c))
  4. if(strlen($a)>0 || strlen($b)>0 || strlen($c)>0) if(strlen($ a)> 0 || strlen($ b)> 0 || strlen($ c)> 0)
  5. if(strlen($a.$b.$c)) 如果(strlen的($ A $ B,$ C))
  6. if(strlen($a.$b.$c)>0) 如果(strlen的($一个$ B。$ C)> 0)

I would do something like this: 我会做这样的事情:

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

There is no overhead of a function like strlen etc. The ! 没有像strlen这样的函数的开销! 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? 假设不是通过生成1000个随机字符串并遍历每个选项的设置来找出测试的方法吗? 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 :) 我认为这是一种幼稚的方式,阅读每个函数的本机代码将产生更彻底的答案,但是仍然很容易:)

您忘记了列表中最相关的功能,即emptyisset

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

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