Access a global variable in a PHP function

Access a global variable in a PHP function

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

Stefan Bogdanescu

Founder & Senior Architect

Understanding PHP Scope and Variable Resolution

You might expect that defining a variable in the global script scope makes it immediately available to any function you call later. The logic seems sound: $data exists at the top level, so reading it inside menugen() should be trivial. Yet, when you run the code, the output is an empty bracket pair []. This behavior often trips up developers new to PHP because it contradicts the scope rules of many other languages like JavaScript or Python.

The root cause lies in how PHP handles variable resolution and function scoping by default. In PHP, variables are not implicitly global within functions. When you enter a function body, the language creates an isolated local scope. Inside that scope, $data is treated as an undefined variable. Attempting to access it triggers an Undefined variable: data notice (unless error reporting is suppressed), but even if you silence the warning, the value remains null or empty because PHP cannot magically reach into the global scope to fetch it without explicit instruction.

To fix this, you must explicitly tell PHP which scope context to look in. The standard approach is using the global keyword or the $GLOBALS superglobal array.

Using the global Keyword

The most direct method involves declaring the variable as global inside the function:

<?php
$data = 'My data';

function menugen() {
    global $data; // Explicitly access the global scope
    
    echo "[" . $data . "]";
}

menugen(); // Output: [My data]
?>

By adding global $data, you are creating an alias in the current function's local scope that points directly to the variable defined at the script level. This makes the code readable and clear about intent. However, relying heavily on the global keyword can lead to tight coupling between functions and the global state, which often makes debugging harder as the application grows.

Using $GLOBALS

An alternative is to access variables via the $GLOBALS superglobal array. This array contains references to all variables in the current scope, regardless of where they were defined.

<?php
$data = 'My data';

function menugen() {
    echo "[" . $GLOBALS['data'] . "]";
}

menugen(); // Output: [My data]
?>

While $GLOBALS works well for quick scripts or accessing configuration values, it can be less readable than the global keyword. It also bypasses standard static analysis tools that might expect local variables to be declared locally, potentially hiding scope issues in larger codebases.

The Preferred Modern Approach: Pass as Arguments

In modern PHP development, relying on global state is generally discouraged because it violates the principle of dependency injection. Functions should receive the data they need as arguments rather than hunting for it in a shared pool. This makes unit testing significantly easier and isolates function logic from external state changes.

If you refactor your example to pass $data into menugen(), the code becomes self-contained:

<?php
$data = 'My data';

function menugen($message) {
    echo "[" . $message . "]";
}

menugen($data); // Output: [My data]
?>

This pattern scales much better. If $data changes, you only need to update the call site, not worry about hidden global dependencies. For complex applications, especially those built on frameworks like Laravel, passing context explicitly is the standard practice for maintaining clean architecture.

When Global Variables Are Necessary

There are specific scenarios where accessing global state is unavoidable or acceptable, such as initializing a single instance of a database connection or reading a configuration flag that every part of the app needs. In these cases, tools like service containers or singleton patterns often provide cleaner abstractions than raw global declarations. If you find yourself frequently needing to access global variables, it might be worth revisiting how your application manages state rather than relying on implicit global access.

Our network