Jump to content

Should I be worried about "Undefined Index"s


iarp

Recommended Posts

I'm trying to sort out a piece of code, my development web server is set to show all errors.

 

I'm currently getting 50+ 'Undefined Index' errors upon initial load (they all disappear after form submission since all variables are in use).

 

Should I be worried, and add a if(isset(.....)){...} wrapper or do most people just leave it be?

Link to comment
Share on other sites

I assume then that the error is with POST data? I wouldn't do a if(isset(.....)){...} in every place the error occurs - but instead I would define a variable based upon the POST field and use the variable. In fact, I'd use a function so I can also aply trim(0 to the value or anything else I need to do.

 

function getPostVar($varname)
{
    if(isset($_POST[$varname]))
    {
        return trim($_POST[$varname]);
    }
    return '';
}

$name = getPostVar('name');
$email = getPostVar('email');
$phone = getPostVar('phone');
//etc.

 

Just an example.

Link to comment
Share on other sites

The function idea is very smart, I shall do that. The script currently has both GET's and POST's.

 

I was just hoping that servers set to not show errors wouldn't be slowed down (i.e. skip the errors). But if that is the case I shall fix them.

Link to comment
Share on other sites

Another important reason for fixing the errors is on a live server, where you would be logging any php errors instead of displaying them, you wouldn't want the additional slow down due to the disk writes to the error log file and you wouldn't want a Gigabyte size error log file to sift through to find and fix an actual problem that is occurring in your code.

Link to comment
Share on other sites

Digging up my original post.

 

After a while of recoding things to fix the Undefined Index issues, i've run into others.

 

For the most part, this is my getVar function:

function getVar($varname) {

if(isset($_GET[$varname])) {
	return $_GET[$varname];
} elseif (isset($_POST[$varname])) {
	return $_POST[$varname];
} else {
	return false;
}

}

 

I don't have to worry about both GET and POST because it's only one or the other for all fields anyways and this function seems to be working well enough.

 

My issue is that i have a lot of short if's that are checking for an array entry such as:

$html .= ($v['readonly']) ? ' readonly="readonly"' : NULL;
$html .= ( ($v['uri']) && ($this->getVar($v['uri'])) ) ? urldecode($_GET[$v['uri']]) : NULL;

 

And it is now these empty array values that are causing the index issues.

 

Do I fix these short ifs somehow or figure out how to set all defaults(if not present) in the array items themselves (somehow).

Link to comment
Share on other sites

Using mjdamato's function will work. But you are using the variables before the function is being called.

 

You get an "undefined index" because you have uninitialized array indexes.

 

An example of this would be:

 

$b = 1;

 

$c = $a + $b;

 

What would the result be? We don't know what $a is right? So the compiler throws a warning and initializes it to zero. That may not be correct, which is why you get an warning.

 

The easiest way to get rid of these errors is to initialize it. mjdamato's function initializes it to either blank or the posted value. But you would have to do it in this manner:

 

$v['uri'] = $this->getVar('uri'); // This is the initialization.
$html .= ($v['uri']) ? urldecode($_GET[$v['uri']]) : NULL;
[code]

You should always initialize your variables. Initializing can also help PHP (or at the very least help you) determine what type of variable it is. For example:

$b = 0; // Initializes to int.
$b = false; // Initializes to bool
$b = ""; // Initializes to string

Link to comment
Share on other sites

Using mjdamato's function will work. But you are using the variables before the function is being called.

The easiest way to get rid of these errors is to initialize it. mjdamato's function initializes it to either blank or the posted value. But you would have to do it in this manner:

 

$v['uri'] = $this->getVar('uri'); // This is the initialization.
$html .= ($v['uri']) ? urldecode($_GET[$v['uri']]) : NULL;

 

No offense, but that makes no sense. The purpose of the function is to return the value of the POST/GET var or an empty string. So, there's no need for that second line to implement a ternary operator. Since urlencode() of an empty string is still an empty string you can just do this

$html .= urldecode($this->getVar('uri'));

 

Also, that function is more of a proof of concept. You don't always want to use an empty string as the default value. in some cases you may want 0 or something else. So, another twist would be to pass an additional parameter to the function for the default

 

function getPostVar($varname, $default)
{
    if(isset($_POST[$varname]))
    {
        return trim($_POST[$varname]);
    }
    return $default;
}

 

Again, this is just a conceptual solution. Your mileage may vary.

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.