0

Before you ask, yes I have searched through nearly 20 different "undefined index" questions on SO before asking this question. Sadly none of them managed to even give me a clue to fixing the problem. I have a simple index.php file that switches through different html files depending on what link you press. On a live server that seems to be no problem at all but when I use Xampp, I receive a long list of "undefined indexes" and I can't figure out why. My Index.php file below. The undefined index message states that "Undefined Index rv in each line."

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

I'm not looking for a way to suppress the error message but an understanding as to why it appears in on my Xampp server only and how to fix it in the future.

  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – mario Sep 03 '13 at 00:42
  • Copy the input to a temporary variable *once*, then compare. Preferrably use a `switch` or array map instead. – mario Sep 03 '13 at 00:43
  • The message is clear enough: the variable $_GET['rv'] is not set. As to why that is, you'd have to look at the method you're using to link to this page and make sure you have `rv=something` in the query string. –  Sep 03 '13 at 00:43
  • `On a live server that seems to be no problem` That's because on the live server, you have warnings disabled. If you turn on error_reporting, you should again see the errors you see, just like on XAMPP. – Dave Chen Sep 03 '13 at 00:59
  • The notice should have a file name and a line number in it. Use that information to narrow down the problem. – Sverri M. Olsen Sep 03 '13 at 01:02
  • Try running [`error_reporting(0)`](http://php.net/manual/en/function.error-reporting.php) on your XAMPP. You should no longer see the warnings. – Dave Chen Sep 03 '13 at 01:02
  • possible duplicate of [how to remove warning messages in php?](http://stackoverflow.com/questions/1987579/how-to-remove-warning-messages-in-php) – Dave Chen Sep 03 '13 at 01:05
  • @SverriM.Olsen Every line in which 'rv' is present contains the error. – Arthur Hargrave Sep 03 '13 at 01:10
  • @ArthurHargrave I know. By using the file name and line number, and the notice's message ("Undefined index"), it should be obvious what is wrong: The index `'rv'` is undefined on every single line. In other words, you need to make sure that it is set using `isset()`. – Sverri M. Olsen Sep 03 '13 at 01:14

2 Answers2

0

That error appears when you try to access a non-existing key in an array. As an example, let's make a simple array:

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

If you try to access the "a" key, it will run without any errors:

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

However, if you try to access a key that has not been set, a "Undefined Index" error will occur:

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

In order to fix this, ensure that the "rv" key exists before accessing it. The most commonly used function to ensure that a key is set is isset:

if(!isset($_GET['rv'])){
    echo 'Oops, it seems like that key is not set!';
    exit;
}
Ghabriel Nunes
  • 372
  • 1
  • 8
0

Check if $_GET['rv'] is set

<?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");
}
?>
Jigar
  • 3,256
  • 1
  • 30
  • 51