【问题标题】:How do I fix "Undefined variable" error in PHP?如何修复 PHP 中的“未定义变量”错误?
【发布时间】:2013-12-21 21:50:24
【问题描述】:
今天,我开始学习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";
?>
我在浏览器中运行此文件时发现以下错误。
注意:未定义变量:/opt/lampp/htdocs/anand/php/index.php 第 19 行中的 x
注意:未定义变量:第 29 行 /opt/lampp/htdocs/anand/php/index.php 中的 y
我该如何解决这个问题?
【问题讨论】:
标签:
php
error-handling
compiler-errors
syntax-error
runtime-error
【解决方案1】:
将 $x 放在 "" 之外的代码中,例如 echo" 变量 $x 是:".$x;
【解决方案2】:
代码的行为符合预期,但如果您想在脚本中同时使用这两个变量,请使用:
<?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";
?>
【解决方案3】:
全局使用变量有两种情况:
- 使用该变量的单个副本并从任何地方修改它,即从函数内部或外部(即在全局范围内)进行修改。在这种情况下,您需要在允许的函数集中以
global $x; 的形式声明。
- 如果您需要单个函数的局部变量,且全局变量使用相同的标识符(即所有函数之外的变量);在这种情况下,您有两个具有相同名称的变量,即该函数的一个本地变量和一个全局变量。然后你需要使用 superglobal 变量
$GLOBALS 即所有全局变量的数组。我个人更喜欢这种方法来编写高效的代码;
以下是两者的代码。
代码 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";
?>
代码 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";
?>
对于REFERENCE。
【解决方案4】:
您必须了解 PHP 中变量的范围。请参阅Variable scope。
在您的代码中,$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";
}
对于 $y,要么通过检查 isset($y) 跳过该输出,要么在全局范围内分配一个默认值。
【解决方案5】:
在 PHP 中,如果要在函数中使用全局变量,则必须在函数中声明为全局变量。
function myTest()
{
$y = 10; // Local scope
global $x;
.....
}
通过在函数中声明 $x 全局,它将引用变量的全局版本。
【解决方案6】:
将 $x 设置为全局,比如
global $x;
或者试试这个:
<?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";
?>
【解决方案7】:
<?php
$a = 1; /* Global scope */
function test()
{
echo $a; /* Reference to local scope variable */
}
test();
?>
您收到第一个错误是因为变量 $a 无法访问全局变量的值,除非您在函数内显式声明 global $a。
示例 #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;
?>
最后一个错误是因为 $y 是在函数 mytest() 中定义的,所以它的范围将仅限于该函数。
如需详细说明,请阅读Variable scope。
【解决方案8】:
第一个错误($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";
}
否则,PHP 无法判断您是否使用同名的局部变量来隐藏全局变量。
第二个错误($y 未定义)是因为本地范围就是这样,本地。它的全部意义在于$y 不会从函数中“泄漏”出来。当然,您不能稍后在代码中访问$y,在定义它的函数之外。如果可以的话,它与全局没有什么不同。