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
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']){

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.