以下代码加载指定文件夹(单独定义)中找到的所有.php文件。有没有办法将它放入数组来简化代码?有没有办法将这个PHP放入数组并简化它?
只有几个变量发生了变化,但本质上代码重复了好几次。
// The General Files
$the_general = opendir(FRAMEWORK_GENERAL);
while (($the_general_files = readdir($the_general)) !== false) {
if(strpos($the_general_files,'.php')) {
include_once(FRAMEWORK_GENERAL . $the_general_files);
}
}
closedir($the_general);
// The Plugin Files
$the_plugins = opendir(FRAMEWORK_PLUGINS);
while (($the_plugins_files = readdir($the_plugins)) !== false) {
if(strpos($the_plugins_files,'.php')) {
include_once(FRAMEWORK_PLUGINS . $the_plugins_files);
}
}
closedir($the_plugins);
还有几个部分调用不同的文件夹。
任何帮助,非常感谢。
干杯, 詹姆斯
理论上你可以有一个名为'something.php.jpg'的文件,所以使用'substr($ file,-4)'可能会比'strpos(...)'更好。 – phidah 2010-07-18 14:42:05
在这种情况下,最好使用'pathinfo($ file,PATHINFO_EXTENSION)'。 – Znarkus 2010-07-18 15:24:51