Problem accessing a global variable in PHP

The following is a snippet of code:

function something() { $include_file = 'test.php'; if ( file_exists($include_file) ) { require_once ($include_file); // global $flag; // echo 'in main global scope flag='.$flag; test(); } } something(); exit; //in test.php $flag = 4; function test() { global $flag; echo '<br/>in test flag="'.$flag.'"'; if ($flag) { echo 'flag works'; //do something } } 

The above code snippet correctly evaluates the value of the global flag $ flag, but does not recognize the $ flag with a value of 4, takes null for the $ flag. Please indicate what is wrong with accessing this global variable $ flag.

Thanks in advance, Anitha

+6
php global-variables
source share
6 answers

$flag = 4; not included in the global area.

If the inclusion occurs inside the function in the calling file, then all the code contained in the called file will behave as if it were defined inside this function.

- The PHP manual page for include , which also applies to include_once, require, and require_once

I am going to assume that the error you are getting is in the line if ($flag) , because at that moment the $ flag is not initialized, because the global $ flag variable has never been assigned a value.

By the way, echo 'global scope flag='.$flag; also does not display the global flag, since you need global $flag; in this function to display a global copy, which also has the side effect of $flag = 4; affects the global copy in the included file.

+2
source share

You have a problem here, simply because PHP is currently interpreting your file. You have nested functions.

Here's how your code runs:

 function something() { $include_file = 'test.php'; if ( file_exists($include_file) ) { //require_once ($include_file); $flag = 4; function test() { global $flag; echo '<br/>in test flag="'.$flag.'"'; if ($flag) { echo 'flag works'; //do something } } //end require_once ($include_file); test(); } } something(); exit; 

As you can see, when you assign the value 4 to $flag ( $flag = 4 ), you fall into the scope of the something() function, but not within the global scope.

In test() , since you declare $flag global within this function, $flag is a completely different variable, global for the entire script.

To avoid this problem, use superglobal $GLOBALS . This is faster than using global anyway, and thus you are not confused about scoping, as indicated above:

 function something() { $include_file = 'test.php'; if ( file_exists($include_file) ) { //require_once ($include_file); $GLOBALS['flag'] = 4; function test() { global $flag; echo '<br/>in test flag="'.$GLOBALS['flag'].'"'; if ($GLOBALS['flag']) { echo 'flag works'; //do something } } //end require_once ($include_file); test(); } } something(); echo $flag; //echos 4 exit; 
+7
source share

The line that probably gives you problems is this: echo 'global scope flag'=$flag;

Change it to: echo 'global scope flag = '.$flag;

Edit: Now that you have edited the code snippet by wrapping the code in the something() function, the problem becomes more clear. I think what happens here is that when you initialize the $ flag with $flag = 4; , its scope is inside the something() method.

Try to place $flag = 4; above / before the line function something() { . Then everything will be right with the world.

+2
source share

Should not echo 'global scope flag'=$flag; be echo 'global scope flag =' . $flag; echo 'global scope flag =' . $flag;

I got another error. This helped me quickly solve the problem.

Parse error: parse error, unexpected '=', expecting ',' or ';' in D: \ Websites (Apache) \ Test Site1 \ test.php on line 6

0
source share

Just do something like this:

 $myFlag = getFlag(); echo $myFlag; // output: whatever // test.php $flag = 'whatever'; function getFlag(){ global $flag; return $flag; } 

I would never set a variable in one script and call it from another file. this will cause extreme confusion along the way. Even my decision is not the best. I would do it as a full-fledged class with an ideal.

0
source share

Since your original question has already been given, let me suggest a different approach that avoids global variables. It seems you have a function + default value that you set as a global variable. You can also define default values ​​for function parameters .

 <?php // test.php function test($flags=4) { echo '<br/>in test flag="'.$flags.'"'; if ($flags!=0) { echo ' flag works'; } } 

There is no global variable, but you can still call test () without a parameter, and the function will use the default value, which you can overwrite by passing the parameter.

 function something() { $include_file = 'test.php'; if ( file_exists($include_file) ) { require_once ($include_file); echo "\nwithout parameter: "; test(); echo "\nwith a parameter: "; test(16); } } something(); exit; 

prints

 without parameter: <br/>in test flag="4" flag works with a parameter: <br/>in test flag="16" flag works 

This is not exactly the same behavior, but it may be what you wanted in the first place ....

edit: Another point could be

 if ( file_exists($include_file) ) { require_once ($include_file); 

which you use. I do not have a strong opinion (or any convincing evidence ;-)), but you can consider using

 $something = include $include_file; if ( false!==$something ) { /* error handling here... */ } 

instead of this. include "returns" false if php cannot include the file. All that is required for this is that the included file does not explicitly return FALSE. This may be more cache friendly (for example, when using something like APC ). But then again: I have no evidence of this, just a hunch. At the very least, it gives your code the ability to handle the error gracefully.

0
source share

All Articles