如何修复 PHP 中的“未定义变量”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20391807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:08:54  来源:igfitidea点击:

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

phperror-handlingcompiler-errorssyntax-errorruntime-error

提问by Anand Mistry

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。而且,我已经创建了我的第一个 PHP 文件来测试不同的变量。你可以看到我的文件如下。

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

注意:未定义变量:第 19 行 /opt/lampp/htdocs/anand/php/index.php 中的 x

注意:未定义变量:y in /opt/lampp/htdocs/anand/php/index.php on line 29

How can I fix the issue regarding it?

我该如何解决有关它的问题?

回答by meagar

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

第一个错误($x未定义)是因为默认情况下全局变量未导入到函数中(与“超级全局变量”相反)。

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

您需要告诉您的函数您正在引用全局变量$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.

否则,PHP 无法判断您是否使用同名的局部变量隐藏全局变量。

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

第二个错误($y未定义)是因为本地范围就是本地范围。它的全部意义在于$y不会“泄漏”出函数。当然,您不能$y在代码中稍后访问定义它的函数之外的内容。如果可以,它与全局没有什么不同。

回答by himanshu bhardiya

Set $x as a global, like

将 $x 设置为全局,例如

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";
?>

回答by R R

<?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 $ainside the function.

您收到第一个错误是因为变量 $a 无法访问全局变量的值,除非您global $a在函数内显式声明。

Example #1 Using a global

Example #1 使用全局

<?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 $yis defined inside the function mytest()so its scope will be limited to that function only.

你得到的最后一个错误是因为$y是在函数内部定义的,mytest()所以它的范围将仅限于该函数。

For a detailed explanation, read Variable scope.

有关详细说明,请阅读变量范围

回答by Rajesh Paul

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 identifierused 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 superglobalvariable $GLOBALSi.e. an array of all the global variables. I personally prefer this approach to make efficient code;
  1. 使用该变量的单个副本并从任何地方修改它,即从函数内部或外部即全局范围内修改。在这种情况下,您需要在表单中允许的函数集中进行声明global $x;
  2. 如果您需要具有用于全局变量的相同标识符的单个函数的局部变量(即所有函数之外的变量);在这种情况下,您有两个同名变量,即该函数的一个局部变量和一个全局变量。然后你需要使用一个超全局变量,$GLOBALS即所有全局变量的数组。我个人更喜欢这种方法来制作高效的代码;

The following are the code for the two.

以下是两者的代码。

Code 1 (using global declaration)

代码 1(使用全局声明)

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

代码 2(使用 $GLOBALS[] 数组)

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

参考

回答by Vipin Kumar Soni

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";
?>

回答by Lingasamy Sakthivel

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

在 PHP 中,如果要在该函数中使用全局变量,则必须在该函数内将其声明为 global。

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.

通过在函数中声明 $x global,它将引用变量的全局版本。

回答by gvgvgvijayan

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

您必须了解 PHP 中变量的作用域。请参阅变量范围

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,

在您的代码中, $x 是一个全局变量,因此为了访问函数内部的变量,请在函数global $x;的开头使用,即,

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.

对于 $y,要么通过检查 isset($y) 跳过该输出,要么在全局范围内分配默认值。