Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with accessing a global variable in PHP

Following is code snippet :

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, echoes 'global scope' $flag value properly but doesnt recognises the $flag with value 4, assumes null value for $flag . Please point out what is wrong in accessing that $flag global variable.

Thanks in advance, Anitha

like image 891
CodeTweetie Avatar asked Feb 18 '10 17:02

CodeTweetie


People also ask

What are the problems with global variables?

Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use. A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code.

How can access global variable in function in PHP?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

What is the best way to declare and access a global variable?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

Why are global variables bad?

Non-const global variables are evil because their value can be changed by any function. Using global variables reduces the modularity and flexibility of the program. It is suggested not to use global variables in the program.


2 Answers

You are running in a problem here simply because the way PHP is currently interpreting your file. You have nested functions.

Here is how your code currently executes:

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 of 4 to $flag ($flag = 4), you are within the scope of the function something(), not within the global scope.

In test(), since you declare $flag as global within that function, $flag is a totally different variable, global to the whole script.

In order to avoid this problem, use the superglobal $GLOBALS. It's faster than using global anyway, and that way you don't get mingled in scoping issues as the ones 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;
like image 103
Andrew Moore Avatar answered Nov 03 '22 05:11

Andrew Moore


$flag = 4; is not in the global scope.

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function.

-- PHP Manual page for include, which also applies for include_once, require, and require_once

I'm going to make a guess that the error you're getting is on the if ($flag) line, because at that point, $flag is uninitialized, because the global $flag variable has never been assigned a value.

Incidentally, echo 'global scope flag='.$flag; isn't displaying the global flag either, as you need a global $flag; in that function to display the global copy, which also has the side effect of making $flag = 4; affect the global copy in the included file.

like image 20
Powerlord Avatar answered Nov 03 '22 05:11

Powerlord