global variables in php not working as expected

Posted by Josh Smeaton on Stack Overflow See other posts from Stack Overflow or by Josh Smeaton
Published on 2008-09-20T09:10:57Z Indexed on 2010/03/19 20:11 UTC
Read the original article Hit count: 325

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about global