• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How do I fix “Undefined variable” error in PHP?

Today, I have started to learn PHP. And, I have created my first PHP file to test different variables. You can see my file as follows.

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

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

    myTest();

    echo "<p>Test variables outside the function:<p>";
    echo "Variable x is: $x";
    echo "<br>";
    echo "Variable y is: $y";
?>

I have found the following errors when I have run this file in the browser.

Notice: Undefined variable: x in /opt/lampp/htdocs/anand/php/index.php on line 19

Notice: Undefined variable: y in /opt/lampp/htdocs/anand/php/index.php on line 29

How can I fix the issue regarding it?

The first error ($x is undefined) is because globals are not imported into functions by default (as opposed to "super globals", which are).

You need to tell your function you're referencing the global variable $x:

function myTest()
{
  global $x; // $x refers to the global variable

  $y=10; // local scope
  echo "<p>Test variables inside the function:<p>";
  echo "Variable x is: $x";
  echo "<br>";
  echo "Variable y is: $y";
}

Otherwise, PHP cannot tell whether you are shadowing the global variable with a local variable of the same name.

The second error ($y is undefined), is because local scope is just that, local. The whole point of it is that $y doesn't "leak" out of the function. Of course you cannot access $y later in your code, outside the function in which it is defined. If you could, it would be no different than a global.

  • 8
Reply Report
<?php
    $a = 1; /* Global scope */

    function test()
    {
        echo $a; /* Reference to local scope variable */
    }

    test();
?>

You are getting the first error because the variable $a can't access the global variable's value unless you explicitly declare global $a inside the function.

Example #1 Using a global

<?php
    $a = 1;
    $b = 2;

    function Sum()
    {
        global $a, $b; // If you want to access a global variable,
                       // you have to use the 'global' keyword

        $b = $a + $b;
    }

    Sum();
    echo $b;
?>

And the last error you are getting because $y is defined inside the function mytest() so its scope will be limited to that function only.

For a detailed explanation, read Variable scope.

  • 2
Reply Report

Set $x as a global, like

global $x;

Or try this:

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

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

    myTest($x);

    echo "<p>Test variables outside the function:<p>";
    echo "Variable x is: $x";
    echo "<br>";
    echo "Variable y is: $y";
?>
  • 2
Reply Report

There are two cases of using a variable globally:

  1. Using a single copy of that variable and modify it from anywhere i.e. modification from within a function or outside i.e. in the global scope. In that case you need a declaration in the allowed set of function in the form global $x;.
  2. In case you need local variables for individual functions with the same identifier used for the global variable (i.e. variables outside all functions); in that case you have two variables with the same name i.e. one local and one global for that function. Then you need to use a superglobal variable $GLOBALS i.e. an array of all the global variables. I personally prefer this approach to make efficient code;

The following are the code for the two.

Code 1 (using global declaration)

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

    function myTest()
    {
        $y = 10; // Local scope
        global $x;

        echo "<p>Test variables inside the function:<p>";
        echo "Variable x in global scope is: $x";
        echo "<br>";
        echo "Variable y is: $y";
    }

    myTest();

    echo "<p>Test variables outside the function:<p>";
    echo "Variable x is: $x";
    echo "<br>";
    echo "Variable y is: $y";
?>

Code 2 (using the $GLOBALS[] array)

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

    function myTest()
    {
        $y = 10; // Local scope
        $x = 23;

        echo "<p>Test variables inside the function:<p>";
        echo "Variable x in global scope is: " . $GLOBALS['x'];
        echo "<br>Variable x in local scope is: $x";
        echo "<br>";
        echo "Variable y is: $y";
    }

    myTest();

    echo "<p>Test variables outside the function:<p>";
    echo "Variable x is: $x";
    echo "<br>";
    echo "Variable y is: $y";
?>

For REFERENCE.

  • 2
Reply Report

The code is behaving as expected, but if you want to use both the variables across the script use this:

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

    function myTest(){
        global $x;
        global $y;
        $y = 10;

        echo "<p>Test variables inside the function:<p>";
        echo "Variable x is: $x";
        echo "<br>";
        echo "Variable y is: $y";
    }
    myTest();

    echo "<p>Test variables outside the function:<p>";
    echo "Variable x is: $x";
    echo "<br>";
    echo "Variable y is: $y";
?>
  • 1
Reply Report

In PHP, global variables must be declared global inside a function if they are going to be used in that function.

function myTest()
{
    $y = 10; // Local scope
    global $x;
    .....
}

By declaring $x global within the function, it will refer to the global version of the variable.

  • 0
Reply Report

You have to learn the scope of a variable in PHP. See Variable scope.

In your code, $x is a global one, so in order to access that variable inside the function use global $x; at the beginning of the function, that is,

function myTest()
{
    global $x;
    $y = 10; // Local scope

    echo "<p>Test variables inside the function:<p>";
    echo "Variable x is: $x";
    echo "<br>";
    echo "Variable y is: $y";
}

For $y, either you skip that output by checking isset($y) or else assign a default value at the global scope.

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions