Change a global variable from inside a function

Change a global variable from inside a function

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

Stefan Bogdanescu

Founder & Senior Architect

Change a Global Variable from Inside a Function

Attempting to modify a global variable directly from within a function often triggers an immediate mental alarm for any developer who has spent time reading code written before modern best practices took hold. In the snippet you provided, the intention is clear: if a date condition is met inside checkdate, the initial value of $var should update to reflect a new year. However, relying on PHP's default behavior for global variables creates scope conflicts that can lead to silent bugs, difficult-to-trace state changes, and code that is harder to test.

When you assign a variable outside of a function, like $var = "01-01-10";, it exists in the global scope. If you try to assign a new value to $var inside checkdate(), PHP does not automatically update the global variable unless you explicitly tell the function to do so using the global keyword or the superglobal array $_GLOBALS. While this works, it couples your internal logic tightly to the global state of the application. This coupling makes the code harder to reason about because a change in one part of the file unexpectedly alters data that other parts rely on.

A more robust approach is to pass the variable into the function as an argument and return the updated value. This pattern keeps the scope local and predictable. Instead of modifying $var globally, you hand it into checkdate(), perform the logic, and then reassign the returned value to your original variable in the global scope.

$var = "01-01-10";

function checkdate($currentDate) {
    // Logic to determine if the condition is met
    // For example, checking if the month/day exceeds a threshold
    if ($currentDate > "01-01-10") {
        return "01-01-11";
    }
    
    // If condition isn't met, return original or null depending on logic
    return $currentDate; 
}

// Capture the result and update the variable explicitly
$var = checkdate($var);

This method eliminates the need for the global keyword entirely. By treating $var as a parameter, you make the dependency explicit in the function signature. When you read the code later, it is immediately obvious that checkdate() requires an input and produces an output. There is no hidden mutation of external state happening behind the scenes.

If you find yourself needing to maintain a single shared state across many functions without passing arguments everywhere, consider using a configuration array or a dedicated state object rather than a loose global string. In larger applications, this might look like a simple associative array holding all necessary data, which is then passed by reference if mutation is absolutely required and unavoidable.

$config = [
    'date' => "01-01-10",
];

function updateDate(&$config) {
    // Check logic here
    $config['date'] = "01-01-11";
}

updateDate($config);
echo $config['date'];

Passing by reference (&$config) allows the function to modify the original array directly, but this is a heavier-weight solution than simple return values. It should be reserved for cases where reassigning every single variable would result in significant clutter or performance overhead.

For projects built on the Laravel framework, managing state is often handled through service providers or dependency injection, which naturally avoids global variables altogether. If you are working within such an ecosystem, injecting the date logic into a class constructor and updating properties there is usually cleaner than reaching for globals. For instance, you might define a DateManager class that handles these checks and returns updated timestamps, ensuring your application remains decoupled from PHP's implicit global scope behavior.

Ultimately, while changing a global variable is technically possible via the global keyword or $_GLOBALS, it is generally an anti-pattern in modern software development. Explicitly passing data into functions and returning results creates code that is easier to test, debug, and refactor.

Our network