我发现自己正在做这种IF
声明分配.例如:
if($variable == 1 || $variable == "whatever" || $variable == '492') { ... }
Run Code Online (Sandbox Code Playgroud)
除了分配时间,我将$变量与4-5个事物进行比较,有时甚至更多.有没有一个简短的方法来写这个?你可以看到重复$variable ==
会变得多余.
我很乐意为此工作,但事实并非如此:
if($variable == (1 || "whatever" || 492) { ... }
Run Code Online (Sandbox Code Playgroud)
Pau*_*aul 10
您可以使用此简写,但请记住,使用或子句显式列出它们的效率较低:
if(in_array($variable, array(1, 'whatever', '492'))){ ... }
Run Code Online (Sandbox Code Playgroud)
此外,如果你想使用===
而不是==
等效的是:
if(in_array($variable, array(1, 'whatever', '492'), TRUE)){ ... }
Run Code Online (Sandbox Code Playgroud)