压缩返回相同值的 GET 变量的代码


Condensing Code for GET Variables That Return The Same Values

我将为不同的URL字符串放置相同的内容,所以我想以某种方式压缩代码。 这是代码

if($_GET['name']=='1'){
    $section = "Box 1";
}
if($_GET['name']=='2'){
    $section = "Box 1";
}
if($_GET['name']=='3'){
    $section = "Box 1";
}
if($_GET['name']=='4'){
    $section = "Box 1";
}

我试过这个,但没有运气:

if($_GET['name']=='1','2','3','4'){
    $section = "Box 1";
}

如何压缩代码,这样我就不会一遍又一遍地重复同样的事情?

if ( in_array($_GET['name'], array(1,2,3,4)) ) {
  $selection = 'Box 1';
}

我认为这应该会有所帮助。

$val = intval($_GET['name']);
if($val >= 1 && $val <= 4 ){
    $section = 'Box 1';
}