php - 在PHP中的函数中加载全局变量

标签 php function variables for-loop crash

我的帖子底部是代码。该脚本自行运行。我想用它来制作有关插件的信息数组(每个插件都有它自己的文件夹)。该文件存储在名为plugins的文件夹旁边。此文件夹包含以下文件夹:

  • File_manager
  • mySQL-工具
  • 网络跟踪器
  • PHP-工具箱
  • 超变量
  • 主题化身

  • 当我运行脚本时,得到以下结果:
    +------------------------------------+
    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/

    相关文章:

    javascript - URL 不会传递来自 ajax 输入的第二个 "select"值

    函数不能返回单个记录项,是吗?任何解决方法?

    python - 在python中多次调用同一个函数

    python - 在 Python 中评估为相等的非等效变量

    php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

    php - symfony Doctrine 类型日期时间预期格式 y-m-d h :i:s

    php - facebook如何将数据推送到news feed?

    php - 使用android php json远程连接mysql数据库?

    excel - 仅当列标题包含特定文本时才使用 COUNTIF

    传递到各个动画的各个元素的 javascript 变量