posfan12
posfan12

Reputation: 2651

Throw error if global variable is not pre-defined?

This is probably a long shot.

I wrote some code in PHP:

function test_dummy()
{
    global $this_is_a_test;
    $this_is_a_test = "test in progress";
}
error_log($this_is_a_test);

However, I did not define the $this_is_a_test global variable before calling test_dummy(). Yet the string "test in progress" is still printed to the log on line 6.

I know why this happens, but my question is: is there a PHP setting to make a function complain/crash if a global variable it expects to already exist is not defined beforehand? My project is large-ish and keeping track of where every single variable is defined and used is getting to be difficult. Spawning an error would be helpful.

Upvotes: 1

Views: 161

Answers (1)

jagad89
jagad89

Reputation: 2643

global keyword is a scope modifier. When you declare global $this_is_a_test; it creates a variable with global scope if it does not exist. So it is working perfectly.

For more details check the documentation.

Upvotes: 2

Related Questions