28

I'm having trouble with global variables in php. I have a $screen var set in one file, which requires another file that calls an initSession() defined in yet another file. The initSession() declares global $screen and then processes $screen further down using the value set in the very first script.

How is this possible?

To make things more confusing, if you try to set $screen again then call the initSession(), it uses the value first used once again. The following code will describe the process. Could someone have a go at explaining this?

$screen = "list1.inc";            // From model.php
require "controller.php";         // From model.php
initSession();                    // From controller.php
global $screen;                   // From Include.Session.inc  
echo $screen; // prints "list1.inc" // From anywhere
$screen = "delete1.inc";          // From model2.php
require "controller2.php"         
initSession();
global $screen;
echo $screen; // prints "list1.inc" 

Update:
If I declare $screen global again just before requiring the second model, $screen is updated properly for the initSession() method. Strange.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • 3
    `global $a;` is equivalent to `$a =& $GLOBALS['a'];`. That is, it creates a reference in local scope with the same name. Outside a function, you're always in global scope. PHP does not provide a "file scope." – Matthew Mar 19 '10 at 20:19

7 Answers7

62

Global DOES NOT make the variable global. I know it's tricky :-)

Global says that a local variable will be used as if it was a variable with a higher scope.

E.G :

<?php

$var = "test"; // this is accessible in all the rest of the code, even an included one

function foo2()
{
    global $var;
    echo $var; // this print "test"
    $var = 'test2';
}

global $var; // this is totally useless, unless this file is included inside a class or function

function foo()
{
    echo $var; // this print nothing, you are using a local var
    $var = 'test3';
}

foo();
foo2();
echo $var;  // this will print 'test2'
?>

Note that global vars are rarely a good idea. You can code 99.99999% of the time without them and your code is much easier to maintain if you don't have fuzzy scopes. Avoid global if you can.

Klaus
  • 724
  • 6
  • 14
Bite code
  • 578,959
  • 113
  • 301
  • 329
  • I wholeheartedly agree with avoiding global 'if you can'. This code was part of a framework we were trying to extend slightly, so we were unable to avoid globals, as we didn't put them there. – Josh Smeaton Oct 30 '10 at 10:12
  • is this true for classes? – Snymax Jun 27 '14 at 23:11
  • globals are super useful for simple scripting, though. Esp. if you have configurations and OO is not appropriate. –  Jan 22 '15 at 20:47
16

global $foo doesn't mean "make this variable global, so that everyone can use it". global $foo means "within the scope of this function, use the global variable $foo".

I am assuming from your example that each time, you are referring to $screen from within a function. If so you will need to use global $screen in each function.

Athena
  • 3,130
  • 1
  • 19
  • 17
6

You need to put "global $screen" in every function that references it, not just at the top of each file.

finnw
  • 47,861
  • 24
  • 143
  • 221
4

If you have a lot of variables you want to access during a task which uses many functions, consider making a 'context' object to hold the stuff:

//We're doing "foo", and we need importantString and relevantObject to do it
$fooContext = new StdClass(); //StdClass is an empty class
$fooContext->importantString = "a very important string";
$fooContext->relevantObject = new RelevantObject();

doFoo($fooContext);

Now just pass this object as a parameter to all the functions. You won't need global variables, and your function signatures stay clean. It's also easy to later replace the empty StdClass with a class that actually has relevant methods in it.

Internet Friend
  • 1,082
  • 7
  • 10
2

You must declare a variable as global before define values for it.

Brynner Ferreira
  • 1,527
  • 1
  • 21
  • 21
  • 1
    What does your answer adds to the previously accepted and highly upvoted answer? Care to comment and add some sample code? – Yaroslav Oct 10 '12 at 20:57
1

The global scope spans included and required files, you don't need to use the global keyword unless using the variable from within a function. You could try using the $GLOBALS array instead.

zobier
  • 1,579
  • 2
  • 13
  • 11
0

It is useless till it is in the function or a class. Global means that you can use a variable in any part of program. So if the global is not contained in the function or a class there is no use of using Global

user3651145
  • 33
  • 1
  • 6