Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 780
Anand Mistry Avatar asked Dec 05 '13 04:12

Anand Mistry


People also ask

How do you fix a undefined variable?

There are a few ways to fix an undefined variable: (1) You can rename the variable or variable set so that it matches what you have used in the topic; (2) you can re-insert the variable in the topic so that it uses an existing variable set/variable name, (3) you can add the undefined variable to the project as a new ...

Why is my variable undefined PHP?

Undefined variable: the variable's definition is not found in the project files, configured include paths, or among the PHP predefined variables. Variable might have not been defined: there are one or more paths to reach the line with the variable usage without defining it.

What is undefined error in PHP?

This error means that within your code, there is a variable or constant that has no value assigned to it. But you may be trying to use the values obtained through the user form in your PHP code. The error can be avoided by using the isset() function.


2 Answers

<?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.

like image 81
R R Avatar answered Sep 19 '22 13:09

R R


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.

like image 20
meagar Avatar answered Sep 20 '22 13:09

meagar