PHP undefined variable

For the following code:

line 10: echo "before require: ".$test; 
line 11: require( dirname(__FILE__) . 'load.php' );
line 12: echo "after require: ".$test; 

Output:

before require: 
Notice: Undefined variable: test in /home/test.php on line 12
after require:

If load.php is very simple, there will be no message: "Note: undefined variable ....". Why and how does "load.php" affect the $ test variable?

+3
source share
5 answers

If the variable is $test unsetinside load.php.

This will affect your variable $testand you will be shown Notice: Undefined variable.

since the variables declared before the include statement will be available inside the included file, therefore, any action performed on the variable inside the included file will affect this variable.

+3
source

Your variable is not set, therefore, a notification.

$test load.php, .

, ... undefined , .

0

:

, , , , . , , . , , .

0

require( dirname(__FILE__) . '/load.php' );?:)

Anyway, it looks like you are either a variable unset()in load.php, or change display_errorsand / or error_reportingto a higher level in load.php.

0
source

If you configured PHP to show all notifications, warnings, and errors, you will see a notification when you run this script: Note: Undefined variable: testing in /path/to/testtype.php on line 3 Notifications are enabled by default when you use php- development.inirather than php-production.ini, and can be very useful when debugging scripts.

0
source

All Articles