Jump to content

Undefined Index Problems


ShoeLace1291

Recommended Posts

So, for the past couple of years or so I've been using CodeIgniter to develop my PHP applications... which may have been a mistake.  As of late, I've been trying to get away from frameworks and am developing an application purely on my own.  I never had this problem with CI, but whenever I try to determine if a variable/index has been defined, I get an error.  For example, if I use an if statement to see if a session index called "mid" was defined, I get a PHP error saying that it's undefined.  Is there any way to get around this?

 

<?php
if($_SESSION['mid']){
     echo $inbox;
} else {
     echo 'Your not logged in!';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/245850-undefined-index-problems/
Share on other sites

When you try to reference a variable that does not exist you may get a warning message depending upon the error level set on the server. The correct way to check if a variable has been set or not is . . . wait for it . . . isset()!

 

if(isset($_SESSION['mid'])){
     echo $inbox;
} else {
     echo 'Your not logged in!';
}

 

However, if you were really wanting to check if the value of $_SESSION['mid'] is TRUE (when it is set), then you might need to do this:

if(isset($_SESSION['mid']) && $_SESSION['mid']){

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.