我正在运行一个PHP脚本,并继续收到错误,如:

注意:未定义变量my_variable_name在C:\wamp\www\mypath\index.php第10行 注意:第11行未定义索引:my_index C:\wamp\www\mypath\index.php 警告:在C:\wamp\www\mypath\index.php第11行未定义数组键“my_index”

第10和11行是这样的:

echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];

这些错误消息的含义是什么?

为什么他们会突然出现?我曾经使用这个脚本多年,从来没有任何问题。

我该如何修复它们?


这是一个一般参考问题,供人们链接作为副本,而不是需要一遍又一遍地解释这个问题。我觉得这很有必要,因为现实世界中关于这个问题的答案都非常具体。 相关元讨论: 如何应对重复的问题? “参考问题”有意义吗?


当前回答

我曾经诅咒过这个错误,但是提醒您转义用户输入是有帮助的。

例如,如果你认为这是聪明的,速记代码:

// Echo whatever the hell this is
<?=$_POST['something']?>

...再想想!更好的解决方案是:

// If this is set, echo a filtered version
<?=isset($_POST['something']) ? html($_POST['something']) : ''?>

(我使用自定义html()函数来转义字符,您的里程可能会有所不同)

其他回答

这意味着您正在测试、求值或打印一个尚未赋值的变量。这意味着你要么有一个拼写错误,要么你需要检查变量是否被初始化为其他东西。检查您的逻辑路径,它可能设置在一个路径,但不是在另一个路径。

这是因为变量'$user_location'没有被定义。如果你在里面使用任何If循环,你声明了'$user_location'变量,那么你也必须有一个else循环并定义相同的。例如:

$a = 10;
if($a == 5) {
    $user_location = 'Paris';
}
else {
}
echo $user_location;

上面的代码将创建一个错误,因为if循环不满足,并且在else循环中没有定义“$user_location”。PHP仍然被要求回显变量。所以要修改代码,你必须做到以下几点:

$a = 10;
if($a == 5) {
    $user_location='Paris';
}
else {
    $user_location='SOMETHING OR BLANK';
}
echo $user_location;

在处理文件时,需要适当的enctype和POST方法,如果表单中没有包含这两者,则会触发未定义的索引通知。

手册规定了以下基本语法:

HTML

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

PHP

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

参考:

POST方法上传

如果使用类,你需要确保你引用成员变量使用$this:

class Person
{
    protected $firstName;
    protected $lastName;

    public function setFullName($first, $last)
    {
        // Correct
        $this->firstName = $first;

        // Incorrect
        $lastName = $last;

        // Incorrect
        $this->$lastName = $last;
    }
}

当我们使用一个未设置的变量时,就会发生这些错误。

处理这些问题的最佳方法是在开发时设置错误报告。

设置错误报告:

ini_set('error_reporting', 'on');
ini_set('display_errors', 'on');
error_reporting(E_ALL);

在生产服务器上,错误报告是关闭的,因此,我们不会得到这些错误。

但是,在开发服务器上,我们可以设置错误报告。

为了消除这个错误,我们看到下面的例子:

if ($my == 9) {
 $test = 'yes';  // Will produce an error as $my is not 9.
}
echo $test;

在赋值或使用变量之前,可以将变量初始化为NULL。

因此,我们可以将代码修改为:

$test = NULL;
if ($my == 9) {
 $test = 'yes';  // Will produce an error as $my is not 9.
}
echo $test;

这不会干扰任何程序逻辑,即使$test没有值,也不会产生Notice。

因此,基本上,在开发时将错误报告设置为ON总是更好的。

并修复所有的错误。

在生产中,错误报告应该设置为关闭。