【问题标题】:Loading global variable in function in PHP在PHP中的函数中加载全局变量
【发布时间】:2014-03-30 00:51:35
【问题描述】:

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

  1. 文件管理器
  2. mySQL 工具
  3. 网络跟踪器
  4. PHP 工具箱
  5. 超级变量
  6. 主题化身

当我运行脚本时,我得到以下结果:

+------------------------------------+
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/>";
    }
}
?>

【问题讨论】:

  • 如果有人能想出一个更好的我的代码版本来绕过这个错误当然也可以。
  • 首先,您不能在 for 循环内定义命名函数。将其移至文件顶部。

标签: php function variables for-loop crash


【解决方案1】:

开启错误报告!!!

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() 移到任何循环之外。

【讨论】:

  • Tnx,我会试试的。并发布结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 2013-03-19
  • 2013-02-21
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多