Kid*_*ond 1 php variables if-statement
我希望最后一个if语句是真的,但它回声说:$ var是type,max或min.
任何人都能解释究竟发生了什么吗?因为我不明白.
$var = 'required';
if($var == 'unique') {
echo '$var is unique.';
} else if($var == ('type' || 'max' || 'min')) {
echo '$var is type, max or min.';
} else if($var == 'required') {
echo '$var is required.';
}
Run Code Online (Sandbox Code Playgroud)
rai*_*7ow 11
你将无法||像这样使用.正确的方法是:
if ($var == 'type' || $var == 'max' || $var == 'min')
Run Code Online (Sandbox Code Playgroud)
如果要检查更多参数,请考虑使用in_array,它会使检查更简洁:
if (in_array($var, ['type', 'max', 'min', 'some', 'other', 'string', 'to', 'check']))
Run Code Online (Sandbox Code Playgroud)