这是我的代码:
$aClass = '';
$bClass = '';
$cClass = '';
if ($_GET['data'] == 'a')
{
$aClass = ' active';
}
else if ($_GET['data'] == 'b')
{
$bClass = ' active';
}
else if ($_GET['data'] == 'c')
{
$cClass = ' active';
}
if ($aClass == '' && $bClass == '' && $cClass == '')
{
$aClass = ' active';
}
有什么想法吗?这似乎很麻烦和多余。
答案 0 :(得分:3)
您可以使用switch-case
构造
switch($_GET['data']) {
case "a": //redundant it is also the default case
$aClass = ' active';
break;
case "b":
$bClass = ' active';
break;
case "c":
$cClass = ' active';
break;
default:
$aClass = ' active';
}
答案 1 :(得分:2)
只有4行;)
if ($_GET['data'] == 'b' || $_GET['data'] == 'c')
${$_GET['data'] . 'Class'} = ' active';
else
$aClass = ' active';
答案 2 :(得分:0)
听起来你正在设置一个css类。如果是这样,你可以尝试
if ($_GET['data'] == 'b')
{
$bClass = ' active';
}
else if ($_GET['data'] == 'c')
{
$cClass = ' active';
}
else
{
$aClass = ' active';
}
答案 3 :(得分:0)
switch ($_GET['data']) {
case 'a': $aClass = ' active';
break;
//ETC....
case default:
$aClass = ' active';
break;
}