Get variables from the outside, inside a function in PHP

Get variables from the outside, inside a function in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Accessing External Variables Inside PHP Functions

You have a variable defined in your global scope and you want to modify or read it inside a function. Your example shows $var initialized to '1', but attempting to increment it within a function fails because the syntax is incorrect for strings, and the scope resolution isn't happening as intended. When you try global $var; and it "doesn't seem to work out," you are likely running into one of two specific scenarios: either you are inadvertently creating a new local variable with that name inside the function, or you are dealing with type coercion issues because PHP treats '1' as a string.

To achieve the goal of returning 2, you must understand how PHP handles scope and type juggling. If $var is declared as a string using single quotes, performing an arithmetic operation like $var + 1 triggers implicit type conversion. PHP will convert the string '1' to the integer 1, perform the addition, and result in 2. However, this does not persist outside the function unless you explicitly return and reassign it.

Here is the corrected logic for your specific snippet:

$var = '1'; // String literal

$result = (function() use ($var) {
    // Implicitly converts string '1' to int 1
    $newValue = $var + 1; 
    return $newValue;
})();

echo $result; // Outputs: 2

In this approach, we utilize the use keyword. This allows the anonymous function to capture the value of $var from the surrounding scope without relying on the global keyword. The use syntax creates a closure that binds the external variable by value. This is generally preferred over using global because it avoids polluting the global namespace and prevents accidental modification of variables defined elsewhere in your application.

If you need to modify the original variable inside the function, simply returning the value isn't enough; you must reassign it back to the parent scope. Using use (&$var) passes a reference to the external variable by address. This means any changes made to $var inside the function will affect the global $var.

$var = '1';

(function() use (&$var) {
    $var = $var + 1; // Modifies the original global variable
})();

echo $var; // Outputs: 2

While referencing external variables provides flexibility, it introduces significant risks. Relying on global or passing variables via references can lead to unintended side effects, making your code harder to test and debug. If multiple functions manipulate $var using references, the final value becomes unpredictable based on execution order.

In larger applications, particularly those built with frameworks like Laravel, this pattern is often unnecessary. Modern PHP development encourages passing dependencies explicitly or injecting them into functions rather than reaching for global state. This practice, known as Dependency Injection, ensures that functions remain pure and their behavior is deterministic. If you are working on a project where maintaining strict separation of concerns is vital, consider refactoring the variable into a class property or passing it as an argument instead of using closures with use.

For complex scenarios involving many external variables, passing them all via the use list can become cumbersome. In such cases, encapsulating the logic within a class where properties hold the state usually results in cleaner architecture than managing a web of global references.

Our network