Php 检查字符串是否等于多个给定字符串中的一个
我想检查字符串是否是给定字符串之一。它看起来像这样Php 检查字符串是否等于多个给定字符串中的一个,php,string,Php,String,我想检查字符串是否是给定字符串之一。它看起来像这样 $string 'test'; // if given string is 'test' function will return false. function checkIfIsSubstring($string) { if (strcmp($string, 'test2') ) return true; if (strcmp($string, 'test3') ) return true; if (strcmp($stri
$string 'test';
// if given string is 'test' function will return false.
function checkIfIsSubstring($string)
{
if (strcmp($string, 'test2') )
return true;
if (strcmp($string, 'test3') )
return true;
if (strcmp($string, 'test4') )
return true;
return false;
}
如果不创建新函数,有没有php函数可以执行相同的操作?您可以将值放入数组中,然后使用:
是,
在_数组中
。文档页面中的第一个示例正是您要做的。您要检查的字符串是否总是相同的?
$array = array('test1', 'test2', 'test3');
$string = 'test1';
if(in_array($string, $array)) {
// do something
}