0

假设我有一个包含 3 个文件的文件夹;文件 1.php、文件 2.php、文件 3.php。在同一个文件夹中,我有 index.php,我想为每个文件生成 switch case。

$files = array();
$folder = opendir(realpath(dirname(__FILE__)));
while ($file = readdir($folder)) { 
    if (strpos($file, '.php') !== false && $file !== 'index.php') { 
        array_push($files, $file);  
    }
}

$switch = $_GET['component'];
switch($switch) {
    foreach ($files as $file) {
        case str_replace('.php', '', $file):        include_once($file);                break;
    }   
}

我希望我的案例最终看起来像什么:

$switch = $_GET['component'];
switch($switch) {
        case file1:     include_once('file1.php');              break;
        case file2:     include_once('file2.php');              break;
        case file3:     include_once('file3.php');              break;
}
4

1 回答 1

4

嗯……对我来说似乎很复杂。

if( in_array($_GET['component'].".php",glob("*.php")) && $_GET['component'] != "index")
    include_once($_GET['component'].".php");
于 2013-05-09T19:10:03.917 回答