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.
That error appears when you try to access a non-existing key in an array. As an example, let's make a simple array:
If you try to access the "a" key, it will run without any errors:
However, if you try to access a key that has not been set, a "Undefined Index" error will occur:
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: