0

我有很多php文件。

要访问每个php我正在使用的参数。

示例 : index.php?route=changepassword, index.php?route=aboutus, 等

我必须route.php将参数链接到正确的文件

它更好地使用

If-Else statement

$route = $_GET['route'];
if ($route==changepassword) {
   //statement
} else if ($route==aboutus) {
   //statement
}

的使用switch-case

$route = $_GET['route'];
switch ($route) {
case "changepassword" :
   //statement
   break;
case "aboutus" :
   //statement
   break;
}

这只是2个文件,我有10多个文件,用什么更好?

4

2 回答 2

4

您应该尽可能使用数组映射:

$map = array(
    "changepassword" => "includes/changepw.php",
    "aboutus" => "templ/aboutus.php",
);

在那种简单的情况下(您没有详细说明您的//statements),您可以将它们用作:

incude( $map[$_GET["route"]] );

通常,您可以将它们映射到类或回调或匿名函数上。

但最后,地图更简洁,更易于维护。

于 2012-11-30T02:38:05.757 回答
1

对于这些情况,如果使用任何一种解决方案都可以获得任何性能,请在性能之前先确保代码清晰。

我肯定会去switch-case

请参阅具有类似答案的其他线程。

于 2012-11-30T02:34:50.490 回答