bomberman Posted May 21, 2014 Share Posted May 21, 2014 (edited) Why does php show undefined variable? I researched and found out that if the variables aren't set then they will likely show undefined variable. However, I used the isset function to check if the variable is undefined and if it is then set it to $varaible ="" . That didn't work and then later i tried $variable = NULL. What should I do Can you please see the code and tell me what shall i do. Thanks a million <?php require_once("includes/connection.php")?> <?php require_once("includes/function.php") ?> <?php require_once("includes/header.php") ?> <?php if(isset($_GET['subj'])){ $sel_subj = NULL; $sel_subj = $_GET['subj']; } elseif (isset($_GET['page'])){ $sel_page = NULL; $sel_page = $_GET['page'] ; } else { $sel_subj= NULL; $sel_page =NULL; } ?> <table id = "structure" > <tr> <td id = "navigation" > <ul class= "subjects" > <?php $subject_set = get_all_subjects(); while ($subject = mysql_fetch_array($subject_set)){ // <a href = "content.php?subj=1" > if ($sel_subj == $subject["id"]){ echo "<li class = \"selected\" "; }else{ echo "<li> "; } "<a href = \"content.php?subj=" . urlencode($subject["id"]) . "\">" . $subject["menu_name"]. "</a></li>" ; $page_set = get_pages_for_subjects( $subject["id"] ) ; echo " <ul class = \"pages\"> "; while ($page = mysql_fetch_array($page_set)){ echo "<li><a href = \"content.php?page=" . urlencode($page["id"]) . "\">" . $page["menu_name"] . "</a></li>" ; } echo "</ul>" ; } ?> </ul> </td> <td id= "page" > <h1> Main Area To Get Your Information </h1> <?php echo $sel_subj ; ?> <br/> <?php echo $sel_page ; ?> <br/> </td> </tr> </table> <?php include ("includes/footer.php") ?> The variable im refering to is $sel_subj and $sel_page at the top of the code. Here is an attachment of the error Edited May 21, 2014 by bomberman Quote Link to comment https://forums.phpfreaks.com/topic/288662-undefined-variable/ Share on other sites More sharing options...
Jacques1 Posted May 21, 2014 Share Posted May 21, 2014 That looks like a couple of C&P errors. In your if statement on top, you overwrite the same variable twice while not setting the other one at all. Quote Link to comment https://forums.phpfreaks.com/topic/288662-undefined-variable/#findComment-1480368 Share on other sites More sharing options...
jazzman1 Posted May 21, 2014 Share Posted May 21, 2014 My logic says to me, <?php if(isset($_GET['subj'])){ $sel_subj = NULL; // to $sel_page = NULL; $sel_subj = $_GET['subj']; } elseif (isset($_GET['page'])){ $sel_page = NULL; //to $sel_subj = NULL; $sel_page = $_GET['page'] ; } else { $sel_subj= NULL; $sel_page =NULL; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288662-undefined-variable/#findComment-1480372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.