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:
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.