我的帖子底部是代码。该脚本自行运行。我想用它来制作有关插件的信息数组(每个插件都有它自己的文件夹)。该文件存储在名为plugins的文件夹旁边。此文件夹包含以下文件夹:
当我运行脚本时,得到以下结果:
+------------------------------------+
Index = 0
Dir = ./plugins/File_manager
Value = 0
+------------------------------------+
Index = 1
Dir = ./plugins/File_manager
Value = 0
+------------------------------------+
Index = 2
Dir = ./plugins/File_manager
Value = 0
+------------------------------------+
Index = 3
Dir = ./plugins/File_manager
Value = 0
+------------------------------------+
Index = 4
Dir = ./plugins/File_manager
Value = 1
+------------------------------------+
Index = 5
Dir = ./plugins/File_manager
Value = 1
Hello worldHello world2
我在上面上面发布的结果实际上不是代码。但是通过这种方式,您可以更好地看到它(否则它将被视为一行大文本)。
如您所见,它无法完成for循环,并且省略了for循环之后的内容。我发现这是因为此行
global $temp, $Dir, $n_index;
。在file_status()
函数中。奇怪的是,代码在重复循环之前一直有效。这是否意味着脚本在崩溃后崩溃?请帮忙。如果有人需要运行代码来帮助我,我可以提供该文件的链接。<?php
//Scan the "plugins" directory for folders but omit the "./" and "../" folder
$array = array_diff( scandir("plugins"), array(".", "..") );
$Plugins_info=array();
for($n=2; $n<=count($array)+1; $n++){
//create temporary array
$temp=array();
//define directory of the plugin
$Dir="./plugins/".$array[$n];
//Set the index value of the array to 0
$n_index=0;
//Function that will put inormation about the files in the array
function file_status($filename){
global $temp, $Dir, $n_index;
echo "+------------------------------------+<br />";
echo "Index = ".$n_index."<br/>";
echo "Dir = ".$Dir."<br/>";
if (file_exists($Dir."/".$filename))$temp[$n_index++]=1;
else $temp[$n_index++]=0;
echo "Value = ".$temp[$n_index-1]."<br/>";
}
$files=array("index.php", "Install.php", "update.php", "uninstall.php", "load.php", "settings.php");
foreach($files as $value){
file_status($value);
}
echo "Hello world";
//Change the plugin name to an array that holds all the plugin information
$Plugins_info[$n-2]=$temp;
echo "Hello world2";
}
echo "Hello world3";
//Output the whole array (each plugin separated by a fancy line)
foreach($Plugins_info as $Temp_array){
echo "+------------------------------------+<br />";
foreach($Temp_array as $Temp_value){
echo "[".$Temp_value."]<br/>";
}
}
?>
最佳答案
打开错误报告!!!
error_reporting(E_ALL);
ini_set('display_errors', '1');
因为函数定义在循环中,所以您获得了
Fatal error: Cannot redeclare file_status() (previously declared in bla bla in bla line bla
。该循环仅执行一次,然后第二次执行,直到您尝试重新定义该功能为止。
将
file_status()
函数移到任何循环之外。
关于php - 在PHP中的函数中加载全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22054174/