global variables in php not working as expected

global variables in php not working as expected

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Global Variables in PHP Not Working as Expected

You are encountering a fundamental misunderstanding of how PHP handles scope, specifically regarding global declarations and variable initialization. When you declare $screen = "list1.inc" in your first file (e.g., model.php) and subsequently call initSession(), the behavior depends entirely on when that global declaration happens relative to the function call.

In your scenario, when you execute:

$screen = "list1.inc";
require "controller.php"; // This likely contains the function definition
initSession();            // This is the function call

The global $screen; statement inside initSession() tells PHP to look up the variable $screen in the Global Symbol Table. Crucially, this happens at runtime. If the script has already executed the line $screen = "list1.inc"; before calling initSession(), the variable exists in the global scope and holds that value. The function successfully retrieves it.

The confusion arises when you attempt to update $screen in a second block:

$screen = "delete1.inc"; // This creates/updates $screen in Global Scope
require "controller2.php"; 
initSession();           // Calls the same function
global $screen;          // Re-declares global reference
echo $screen;            // Prints "list1.inc" ???

If echo $screen prints "list1.inc" despite setting it to "delete1.inc", one of two things is happening: either the second assignment didn't actually occur in a way that persisted, or—and this is more common—the variable $screen was never re-initialized in the Global Symbol Table before the function call.

The Trap of Function Scope vs. Global Scope

The most frequent cause of this specific issue is how PHP handles variables defined inside functions versus those in the global scope. If initSession() is defined like this:

function initSession() {
    global $screen;
    // ... logic using $screen
}

This declaration merely creates a link to the variable named $screen in the calling script's global scope. It does not create a new variable inside initSession. However, if you accidentally define $screen inside a function or class method without declaring it global, PHP treats it as a local variable that shadows the global one.

In your specific case, where the value reverts to "list1.inc", it is highly probable that the second script (model2.php) is not actually executing before the initSession() call, or the variable assignment is happening inside a block that isn't being reached. Alternatively, if you are using an older version of PHP (pre-5.4), there were stricter scoping rules where variables defined in one file but accessed via global in another required strict sequential execution without any intervening function calls that might have reset the context.

However, a more subtle issue often plagues developers working with this pattern: variable initialization order. If controller.php or controller2.php includes files that also define or initialize $screen, those definitions happen before you manually set it to "delete1.inc". This effectively overwrites your intended value.

Why Re-declaring Global Fixes It (Sometimes)

You mentioned: "If I declare $screen global again just before requiring the second model, $screen is updated properly."

This works because the global $screen; declaration inside initSession() forces PHP to resolve the symbol table at that exact moment. If you ensure that $screen = "delete1.inc"; executes before initSession() is invoked, and no other code overwrites it in between, the function will grab the new value.

The reason this feels "strange" is that developers often expect variables to be passed by reference or automatically updated across files. PHP does not work this way; scope is strictly defined by where the variable is declared or assigned.

Best Practices for Session and State Management

Relying on global variables to pass state between files is fragile, especially in larger applications like Laravel or Symfony. The pattern you are describing—setting a variable in one file, requiring another, and using global inside a function—is essentially simulating a dependency injection mechanism manually. This leads to tight coupling and makes debugging difficult, as the value of $screen can change depending on execution order rather than explicit method calls.

A more robust approach is to pass the value explicitly or use a dedicated session service. If you are managing state for a web application, consider using PHP's native $_SESSION superglobal:

// model.php
$_SESSION['screen'] = "list1.inc";

// controller.php
function initSession() {
    // Access directly without 'global' keyword if inside the function scope context
    echo $_SESSION['screen']; 
}

This ensures that the value persists across requests and is managed centrally, avoiding the pitfalls of global variable shadowing and accidental overwrites. If you are migrating to a framework like Laravel, this manual handling of globals is replaced by dependency injection containers and service providers, which handle scope management automatically.

Conclusion on Scope Mechanics

The behavior you observed is not a bug but a direct consequence of PHP's execution model: variables are resolved at the time they are referenced. If the assignment happens after the function call, or if another piece of code resets the variable before the global declaration resolves it, you will see the old value. Ensuring strict execution order and avoiding reliance on global keywords in favor of explicit parameters or session storage will eliminate these inconsistencies.

Our network