PHP未定义变量
对于以下代码:PHP未定义变量,php,variables,undefined,Php,Variables,Undefined,对于以下代码: line 10: echo "before require: ".$test; line 11: require( dirname(__FILE__) . 'load.php' ); line 12: echo "after require: ".$test; 输出: before require: Notice: Undefined variable: test in /home/test.php on line 12 after require: 如果load.ph
line 10: echo "before require: ".$test;
line 11: require( dirname(__FILE__) . 'load.php' );
line 12: echo "after require: ".$test;
输出:
before require:
Notice: Undefined variable: test in /home/test.php on line 12
after require:
如果load.php非常简单,则不会出现消息:“注意:未定义变量…”。“load.php”为什么以及如何影响$test变量 如果
$test
变量在load.php
文件中是unset
这将影响您的$test
变量,并将显示注意:未定义变量
因为在include语句之前声明的变量将在包含的文件中可用,所以对包含的文件中的变量执行的任何操作都将影响该变量。您的变量未设置,因此会显示通知消息 如果在load.php中为变量
$test
设置一个值,它将具有该值
如果你没有,那么。。。在您的情况下,它将保持未定义状态,这不是一个好的做法。来自:
包含文件时,其包含的代码将继承包含文件所在行的代码。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局作用域
它不应该是
require(dirname(_文件)'/load.php')代码>?:)
无论如何,它看起来像是您在load.php
中unset()
变量,或者在load.php
中将display\u errors>和/或error\u reporting
修改到更高级别
如果已将PHP配置为显示所有通知、警告和
错误,运行此脚本时将看到一个通知:通知:
未定义变量:在第3行的/path/to/testtype.php中测试
当您使用php-development.ini时,默认情况下是打开的
比php production.ini更好,在调试时非常有用
脚本
请显示您的load.php
源代码。