比较PHP中的多个值 [英] Compare multiple values in PHP
本文介绍了比较PHP中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从这里出发:
if($var == 3 || $var == 4 || $var == 5 || $var =='string' || $var == '2010-05-16') {
// execute code here
}
对此:
if($var == (3, 4, 5, 'string', '2010-05-16')) { // execute code here }
继续输入$var
似乎很多余,而且我发现阅读起来有点麻烦. PHP中有没有办法以这种方式简化它?我在这里读到一篇文章,当您使用XQuery时,可以像在$var = (1,2,3,4,5)
等中那样使用=运算符.
Seems very redundant to keep typing $var
, and I find that it makes it a bit cumbersome to read. Is there a way in PHP to do simplify it in this way? I read on a post here that when using XQuery you can use the = operator as in $var = (1,2,3,4,5)
etc.
推荐答案
将值放在数组中,然后使用in_array()函数检查它们是否存在.
Place the values in an array, then use the function in_array() to check if they exist.
$checkVars = array(3, 4, 5, "string", "2010-05-16");
if(in_array($var, $checkVars)){
// Value is found.
}
http://uk.php.net/manual/zh/function.in-array.php
这篇关于比较PHP中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文