错误消息注意:在php中包含函数时未定义的变量

时间:2013-12-25 05:42:30

标签: php scope

我有太多单独的PHP文件:

  1. 添加新-module.php
  2. _add-NEW-module.php
  3. 我正在页面顶部的_add-new-module.php中使用函数require_once add-new-module.php。我已将变量$msg初始化为_add-new-module.php,因为某些原因它仍显示错误消息:

    Notice: Undefined variable: msg in xxxxxxxxxxx/add-new-module.php on line 2
    

    当回显$msg中的变量_add-new-module.php时,它会工作,但当我尝试回显$msg中的变量add-new-module.php时,它会给我上面的错误。当我在变量islet上使用$msg时,它会隐藏错误消息,但不会在页面上显示任何内容。

    我还用include_oncerequire_oncerequire替换了包含。仍然没有运气。我还使用全局$msg隐藏了错误消息,但似乎没有显示结果。

2 个答案:

答案 0 :(得分:0)

$msg可能超出范围,请尝试添加

global $msg;

到使用它的页面。

答案 1 :(得分:0)

你可以使用全球$ msg;在add-new-module.php中,只需在echo $ msg之前写入;

 global $msg;
 echo $msg; 

如果不工作在PHP APACHE Server Php.ini中启用全局变量Register_globals

register_globals = On

如果您想在页面中打开它,可以将代码放在代码的开头:

ini_set('register_globals',true);

否则通过.htaccess启用register_globals:

  1. 在虚拟主机的根目录中创建.htaccess
  2. 将以下代码放入其中:

    //覆盖PHP设置。第一个IfModule是 //用于Apache 1.3,第二个用于Apache 2.

     <IfModule mod_php4.c>
         php_value register_globals 1
     </IfModule>
    
     <IfModule sapi_apache2.c>
         php_value register_globals 1
     </IfModule>
    
       php_flag register_globals 1