注意:Xampp中的未定义索引。我该如何解决?

时间:2013-09-03 00:39:37

标签: php xampp undefined-index

在你问之前,是的,在询问这个问题之前,我已经在SO上搜索了近20个不同的“未定义索引”问题。可悲的是,他们都没有设法给我一个解决问题的线索。我有一个简单的index.php文件,根据你按的链接切换不同的html文件。在一个看起来没有问题的实时服务器上,但是当我使用Xampp时,我收到了一长串“未定义的索引”,我无法弄清楚原因。我的Index.php文件如下。未定义的索引消息指出“每行中未定义的索引rv。”

<?php
include ("anime_header.html");

if($_GET['rv'] == "amnesia")         include ("Amnesia.html");
else if($_GET['rv'] == "shingeki")   include ("AttackOnTitan.html");
else if($_GET['rv'] == "chuunibyou") include ("Chuunibyou.html");
else if($_GET['rv'] == "crimeedge")  include ("CrimeEdge.html");
else if($_GET['rv'] == "datealive")  include ("DateALive.html");
else if($_GET['rv'] == "duskmaiden") include ("DuskMaiden.html");
else if($_GET['rv'] == "gargantia")  include ("Gargantia.html");
else if($_GET['rv'] == "K_anime")    include ("K_Anime.html");
else if($_GET['rv'] == "karneval")   include ("Karneval.html");
else if($_GET['rv'] == "kotoura")    include ("Kotoura-San.html");
else if($_GET['rv'] == "kaibutsu")   include ("LittleMonster.html");
else if($_GET['rv'] == "nerawareta") include ("Nerewareta.html");
else if($_GET['rv'] == "redgarden")  include ("RedGarden.html");
else if($_GET['rv'] == "saikano")    include ("Saikano.html");
else if($_GET['rv'] == "sakurasou")  include ("Sakurasou.html");
else if($_GET['rv'] == "sasamisan")  include ("Sasami-San.html");
else if($_GET['rv'] == "vividred")   include ("Vividred.html");
else include ("animereviews.html");

include ("ReviewFooter.html");
?>

我不是在寻找一种方法来抑制错误信息,而是要了解它出现在我的Xampp服务器上的原因以及将来如何解决它。

2 个答案:

答案 0 :(得分:0)

当您尝试访问阵列中不存在的键时,会出现该错误。举个例子,让我们做一个简单的数组:

$myarr = array(
    'a' => 5
);

如果您尝试访问“a”键,它将运行时没有任何错误:

if ($myarr['a'] == 5){
    echo 'Perfect!';
}

但是,如果您尝试访问尚未设置的密钥,则会出现“未定义索引”错误:

if ($myarr['b'] == 6){
    echo 'The previous line should cause an error.';
}

为了解决这个问题,请确保在访问“rv”键之前存在“rv”键。确保设置密钥的最常用函数是isset

if(!isset($_GET['rv'])){
    echo 'Oops, it seems like that key is not set!';
    exit;
}

答案 1 :(得分:0)

检查$ _GET [&#39; rv&#39;]是否已设置

<?php
$rv = $_GET['rv'];
if(isset($rv)){
    include ("anime_header.html");

    if($rv == "amnesia")         include ("Amnesia.html");
    else if($rv == "shingeki")   include ("AttackOnTitan.html");
    else if($rv == "chuunibyou") include ("Chuunibyou.html");
    else if($rv == "crimeedge")  include ("CrimeEdge.html");
    else if($rv == "datealive")  include ("DateALive.html");
    else if($rv == "duskmaiden") include ("DuskMaiden.html");
    else if($rv == "gargantia")  include ("Gargantia.html");
    else if($rv == "K_anime")    include ("K_Anime.html");
    else if($rv == "karneval")   include ("Karneval.html");
    else if($rv == "kotoura")    include ("Kotoura-San.html");
    else if($rv == "kaibutsu")   include ("LittleMonster.html");
    else if($rv == "nerawareta") include ("Nerewareta.html");
    else if($rv == "redgarden")  include ("RedGarden.html");
    else if($rv == "saikano")    include ("Saikano.html");
    else if($rv == "sakurasou")  include ("Sakurasou.html");
    else if($rv == "sasamisan")  include ("Sasami-San.html");
    else if($rv == "vividred")   include ("Vividred.html");
    else include ("animereviews.html");

    include ("ReviewFooter.html");
}
?>