我经常发现自己写的东西 if($var == 'bob' || $var == 'steven || $var == 'james'){ ...
在php中是否有一种更流畅的方式来检查var是否与任何运算符匹配?或者我必须编写自己的函数,如:
function matches_any($question, $pos_answer, $pos_answer, $pos_answer, $pos_answer, ...){
// Call the function args, get the first one, get the rest, compare all the rest, return true if any match, return false by default.
}
Run Code Online (Sandbox Code Playgroud)
因此将使用哪个:
if(matches_any($var, $bob, $steven, 'james', $william)){ ...
Run Code Online (Sandbox Code Playgroud)
您可以使用 in_array()
if(in_array('value_to_test', array($var, $bob, $steven, 'james', $william))) {}
$matches = array($var, $bob, $steven, 'james', $william);
if (in_array('james', $matches)) {
// TRUE
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
290 次 |
| 最近记录: |