我正在运行一个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"];

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

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

我该如何修复它们?


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


当前回答

我问了一个关于这个问题的问题,我被推荐到这篇文章,上面写着:

这个问题已经有了答案: “Notice: Undefined variable”、“Notice: Undefined index”、“Notice: Undefined index” 未定义的偏移量”使用PHP

我在这里分享我的问题和解决方案:

这是错误:

第154行就是问题所在。第154行是这样的

153    foreach($cities as $key => $city){
154        if(($city != 'London') && ($city != 'Madrid') && ($citiesCounterArray[$key] >= 1)){

我认为问题是我正在为变量$city写if条件,它不是键,而是$key => $city中的值。第一,你能否证实这是否是警告的原因?其次,如果这是问题所在,为什么我不能根据值编写条件呢?我需要用键来写条件吗?

更新1:问题是当执行$citiesCounterArray[$key]时,有时$key对应的是一个在$citiesCounterArray数组中不存在的键,但根据我的循环数据,情况并非总是如此。我需要的是设置一个条件,以便如果数组中存在$key,则运行代码,否则跳过它。

更新2:这是我如何通过使用array_key_exists()修复它:

foreach($cities as $key => $city){
    if(array_key_exists($key, $citiesCounterArray)){
        if(($city != 'London') && ($city != 'Madrid') && ($citiesCounterArray[$key] >= 1)){

其他回答

试试这些

Q1:这个通知意味着$varname不是 的当前范围内定义 脚本。 Q2:在使用任何可疑变量之前,使用isset(), empty()条件可以很好地工作。

// recommended solution for recent PHP versions
$user_name = $_SESSION['user_name'] ?? '';

// pre-7 PHP versions
$user_name = '';
if (!empty($_SESSION['user_name'])) {
     $user_name = $_SESSION['user_name'];
}

或者,作为一个快速而肮脏的解决方案:

// not the best solution, but works
// in your php setting use, it helps hiding site wide notices
error_reporting(E_ALL ^ E_NOTICE);

关于会话的注意事项:

当使用会话时,session_start();必须放置在使用会话的所有文件中。 http://php.net/manual/en/features.sessions.php

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

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

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

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

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

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

通常是因为“糟糕的编程”,以及现在或以后出现错误的可能性。

如果这是一个错误,首先对变量进行适当的赋值:$varname=0; 如果它确实只是偶尔定义,在使用它之前测试它:If (isset($varname)) 如果是因为你拼写错了,那就改正 甚至可以在你的php设置中打开警告

在处理文件时,需要适当的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方法上传

HTML表单提交后变量不存在的一个常见原因是表单元素没有包含在<form>标记中:

示例:<表单>中不包含的元素

<form action="example.php" method="post">
    <p>
        <input type="text" name="name" />
        <input type="submit" value="Submit" />
    </p>
</form>

<select name="choice">
    <option value="choice1">choice 1</option>
    <option value="choice2">choice 2</option>
    <option value="choice3">choice 3</option>
    <option value="choice4">choice 4</option>
</select>

示例:元素现在包含在<表单>中

<form action="example.php" method="post">
    <select name="choice">
        <option value="choice1">choice 1</option>
        <option value="choice2">choice 2</option>
        <option value="choice3">choice 3</option>
        <option value="choice4">choice 4</option>
    </select>
    <p>
        <input type="text" name="name" />
        <input type="submit" value="Submit" />
    </p>
</form>