在PHP中高效测试多个值 [英] Testing multiple values efficiently in PHP
本文介绍了在PHP中高效测试多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,所以我需要测试一些可能的比赛.它可以等于 A 或 B ,因此测试它的明显方法是这样的:
Okay, so I have a few possible matches that I need to test. It can either equal A or B, so the obvious way to test it would be something like this:
if($val=="A"||$val="B"){
echo "yup";
}
我只是想知道它们是否是一种更简单的测试值的方法,而无需为每个值重新声明变量,就像这样(我知道这是行不通的):
I was just wondering if their were an easier way to test values without restating the variable for every value, like this (I know this doesn't work):
if($val==("A"||"B")){
echo "yuppers";
}
这样可能吗?
推荐答案
您可以使用in_array
$array = array('A','B','other values');
if(in_array($val, $array)){
// value is in array
}else {
// invalid value
}
这篇关于在PHP中高效测试多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文