具有多个值的PHP if语句
有没有更简短的方式来写这个具有多个值的PHP if语句,php,if-statement,Php,If Statement,有没有更简短的方式来写这个 <? if($_GET['id']==1 || $_GET['id']==3 || $_GET['id']==4 || $_GET['id']==5) {echo 'does it really have to be this explicit?'}; ?> 也许是这样的 <? if($_GET['id']==1 || 3 || 4 || 5){echo 'this is much shorter'}; ?> 只需尝试: if (
<?
if($_GET['id']==1 ||
$_GET['id']==3 ||
$_GET['id']==4 ||
$_GET['id']==5)
{echo 'does it really have to be this explicit?'};
?>
也许是这样的
<?
if($_GET['id']==1 || 3 || 4 || 5){echo 'this is much shorter'};
?>
只需尝试:
if ( in_array($_GET['id'], array(1, 3, 4, 5)) ) {}
也许这种转变会有所帮助
switch($_GET['id']) {
case 1:
case 3:
case 4:
case 5:
echo 'Slect maybe :P';
break;
}
声明一个数组:
$values = array(1,3,4,5);
获取您的变量
$id = $_GET['id'];
现在在_array()中使用PHP
阅读有关的内容可能不短,但可读性更强。请尝试使用in_array()函数:
if (in_array($_GET['id'], array(1, 3, 4, 5)))
{
echo "What about this?";
}
可以使用如下所示的正则表达式:
preg_match(['1-4']);
这是我知道的最短的路。另外,它更容易阅读。我不知道函数
select()
-你能给我指一下php.net上的手册吗?谁投票支持这一点????
preg_match(['1-4']);