更新时间:2023-11-29 16:53:29
逻辑||
(OR) 运算符 无法正常工作.||
运算符的计算结果始终为布尔值 TRUE 或 FALSE.因此,在您的示例中,您的字符串被转换为布尔值,然后进行比较.
The logical ||
(OR) operator doesn't work as you expect it to work. The ||
operator always evaluates to a boolean either TRUE or FALSE. So in your example your strings get converted into booleans and then compared.
If 语句:
if($ext == ('txt' || 'rtf'|| 'log' || 'docx'))
归结为:
if($ext == (TRUE || TRUE || TRUE || TRUE))
if($ext == TRUE)
要解决这个问题并使代码按照您的意愿工作,您可以使用不同的方法.
To solve this problem and get the code to work as you want it to you can use different methods.
解决问题并根据多个值检查您的值的一种方法是,实际将值与多个值进行比较:
One way to solve the problem and check your values against multiple values is, to actually compare the value against multiple values:
if($ext == "txt" || $ext == "rtf" /* || ... */)
in_array()
另一种方法是使用函数 in_array()
并检查该值是否等于数组值之一:
in_array()
Another way is to use the function in_array()
and check if the value is equal to one of the array values:
if(in_array($ext, ["txt", "rtf" /* , ... */], TRUE))
注意:第二个参数用于严格比较
您也可以使用 switch
来根据多个值检查您的值,然后让案例失败.
You could also use switch
to check your value against multiple values and just let the case fall through.
switch($ext){
case "txt":
case "rtf":
/* case ...: */
$pClass = "text-";
break;
}