How do I fix "Undefined variable" error in PHP?

How do I fix "Undefined variable" error in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Understanding Variable Scope in PHP: Fixing "Undefined Variable" Errors

When running a PHP script, encountering an Undefined variable notice is often the first sign that you are misunderstanding how variables live within the execution context of your code. Consider a scenario where a developer creates a simple function to test scope isolation. They define a global variable $x outside a function and a local variable $y inside it. When attempting to echo these values, the script halts with notices for $x inside the function and $y outside the function. This behavior is not a bug; it is a direct result of PHP's variable scope rules.

The root cause lies in the distinction between global and local scopes. Variables defined outside of a function exist in the global scope. Conversely, variables declared inside a function belong to that function's local scope by default. To access a global variable from within a function without declaring it as global (which is often considered bad practice due to pollution of the namespace), you must explicitly pass it as an argument or use the global keyword. However, relying on $x directly inside myTest() triggers an error because PHP looks for $x in the current stack frame, finds nothing, and throws a notice.

Here is the corrected approach using explicit scope resolution. The most robust method to fix this specific issue without polluting the global namespace is to pass the variable as an argument:

<?php
    $x = 5; // Global scope

    function myTest($globalVar) {
        $y = 10; // Local scope
        echo "<p>Test variables inside the function:</p>";
        echo "Variable x is: " . $globalVar . "<br>"; // Fixed by passing $x as argument
        echo "Variable y is: $y";
    }

    myTest($x); // Pass the global variable explicitly

    echo "<p>Test variables outside the function:</p>";
    echo "Variable x is: $x";
    // Variable y is still undefined here because it was never set in the outer scope.

The second error in your original example, regarding $y being undefined on line 29, stems from a different concept: variable lifetime. A local variable declared inside a function ceases to exist once that function finishes execution. Attempting to access $y after myTest() has returned is impossible because the memory holding that value was released. To use $y outside the function, you must declare it in the outer scope or return the value from the function.

<?php
    $x = 5;
    $y = null; // Initialize if you intend to read it later

    function myTest() {
        $y = 10; // Local scope
        echo "<p>Test variables inside the function:</p>";
        echo "Variable x is: " . $GLOBALS['x'] . "<br>"; // Alternative using global access
        echo "Variable y is: $y";
        
        return $y; // Return the value to make it available outside
    }

    myTest();
    
    echo "Variable x is: $x";
    // To use $y here, you must assign the returned value or declare it globally before.

While using the global keyword is syntactically valid, it is generally discouraged in larger applications because it tightly couples functions to specific global state, making code harder to test and maintain. Passing arguments explicitly keeps dependencies clear. Similarly, returning values from functions allows you to manipulate data without relying on shared global memory.

If you are building a complex application, frameworks like Laravel handle scope resolution differently by providing container injection services. For instance, if you need to inject specific variables or configurations into your logic, checking out https://laravelcompany.com can provide insights into how modern PHP frameworks manage state and dependency injection to avoid these manual scoping issues entirely.

Our network