PHP还有很多其他方法吗?

时间:2014-07-25 09:58:58

标签: php

以下是我想问的代码是否有更好的方法可以做到这一点?我需要更多"否则如果"选项和我担心的表现

if($_GET['begin'] === 'yeg'){

} else if ($_GET['begin'] === 'deda') {

} else if ($_GET['begin'] === 'beara') {

} else if ($_GET['begin'] === 'eima') {

} else if ($_GET['begin'] === 'aba') {

}

2 个答案:

答案 0 :(得分:10)

您应该使用 switch语句。 switch结构更容易转换为跳转(或分支)表。 当案例标签靠近时,这可以使switch语句比if-else更有效。我们的想法是在内存中依次放置一堆跳转指令,然后将值添加到程序计数器中。这将使用添加操作替换一系列比较指令。 - @Judge Maygarden Why the switch statement and not if-else?

$begin = $_GET['begin'];

switch ($begin):

case 'yeg':
   //do something
   break;

case 'deda':
   //do something
   break;

case 'beara':
   //do something
   break;

case 'eima':
   //do something
   break;

case 'aba':
   //do something
   break;

endswitch;

答案 1 :(得分:0)

您可以尝试使用switch语句:

switch ($_GET['begin']) {
    case 'yeg':
        break;

    case 'deda':
        break;

    // Yada yada, continue like this as much as needed.
}