有没有办法将这个PHP放入数组并简化它? [英] Is there a way to put this PHP into an array and simplify it?
本文介绍了有没有办法将这个PHP放入数组并简化它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码加载在指定文件夹(单独定义)中找到的所有 .php 文件.有没有办法把这个放到数组中来简化代码?
The following code loads all .php files found in the specified folder (defined separately). Is there a way to put this into an array to simplify the code?
只有几个变量发生了变化,但基本上代码重复了几次.
Only a couple of variables change but essentially the code repeats several times.
// 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);
还有几个部分调用不同的文件夹.
There are several more sections which call different folders.
非常感谢任何帮助.
干杯,詹姆斯
推荐答案
我更好的方法是使用 glob().并把它变成一个函数.
I nicer way to do this would to use glob(). And make it into a function.
function includeAllInDirectory($directory)
{
if (!is_dir($directory)) {
return false;
}
// Make sure to add a trailing slash
$directory = rtrim($directory, '/\\') . '/';
foreach (glob("{$directory}*.php") as $filename) {
require_once($directory . $filename);
}
return true;
}
这篇关于有没有办法将这个PHP放入数组并简化它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文