[英]Abbreviate php if/or statement
is there a way to abbreviate the following? 有没有办法缩写以下内容?
if ($chk == 1 || $chk == 3 || $chk == 5 || $chk == 7){
do some stuff
}
Thanks. 谢谢。
if (in_array($chk, array(1, 3, 5, 7))) ...
Or, if you plan to include if-elseif-cascades, use switch statement: 或者,如果您计划包含if-elseif-cascades,请使用switch语句:
switch($chk)
{
case 1: case 3: case 5: case 7:
do_something();
break;
case 10: case 30: case 50: case 70:
do_something_else();
break;
default:
do_default();
}
If you use only uneven numbers, you could check the value. 如果仅使用不均匀的数字,则可以检查该值。 If the value is uneven, then the function will be executed
如果值不均匀,则执行该功能
$unevenNumbers=array(1,2,3,4,5,7);
foreach($unevenNumbers as $nbr){
if( $nbr % 2 !== 0){
//do some stuff
}
}
if you are looking odd! 如果你看起来很奇怪!
if (!(($chk % 2)==0) // can use if($chk&1) instead
{
//odd acess
}
else
{
//even stuff
}
or 要么
if($num&1)
{
//odd stuff
}
else
{
//even stuff
}
or some thing logic 或者某种逻辑
if($num&1)
{
//odd stuff
if($num>=1 && $num<=7)
{//your special stuff
}
}
else
{
//even stuff
}
您可以使用三元运算符。
in_array($chk, array(1, 3, 5, 7)) ? /* Do some stuff*/;
<?php
$arr = array(1, 3, 5, 7);
if (in_array($chk, $arr)) {
// DO SOMETHING
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.