Page 1 of 1

Undefined Index - Design Error?

Posted: Sun Mar 06, 2005 5:00 pm
by Ambush Commander
For any person who runs full error reporting, the warning produced by Undefined Variable and Undefined Index are some of the biggest plagues upon the web. As an novice programmer, I think that I've fallen into this trap: my scripts are inundated with a river of Warnings thrown by Undefined variables when I set Error Reporting on high.

However, it's not a simple case of forgetting to set $i = 0 when I start a simple loop. The way my program works, it loads a few, special of variables into the global namespace in names such as $sinf, $sc etc. These are arrays that contain more information. Then, I operate on the information, manipulating it for the main display. There's a lot of tricky stuff going on.

The problem is that I can't be exactly sure what values will be loaded and which will not. Currently, the program is written on the assumption that variables in an array that are undefined contain empty values. Some problems are that when I'm testing to see whether or not a particular variable is empty or not, when it's entered into a function that tests for definedness, they run into problems, because PHP throws an error if you even refer an undefined index to a function.

I'm starting to think that this is a fundamental design flaw, that I shouldn't have constructed the logic this way, but I don't want to rewrite my entire script. I've been thinking about a few solutions, such as:
  • Taking all the functions that utilize the variable variables, test out the variable pool and change the arguments based on what's set and what's not. But I use a lot of functions, and this will cause a lot of code creep and extra stuff
    Taking the script that creates the variables from the database and assigning blank values (but not undefined) to the variables that are most commonly assigned as arguments. This might not work if I use very specific variables for some functions (which means I may have to redefine the arguments so that they only take the general variables)
How do I program in the future to avoid this trap? How do you suggest I rectify this problem? Thanks in Advance. If anyone wishes to see the code, I'd be more than happy to post it.

Posted: Sun Mar 06, 2005 5:21 pm
by John Cartwright
Basically what I try to keep in mind is don't use a variable unless it has been surely predefined, and if not, assign it a design value. So let's say I am running into a situation where the variable may or may not be defined yet, design flaw? maybe. Should be avoid? yes.

Generally, when I don't know if a variable is going to be defined, especially with $_POST and $_GET, I create a function that will check for a value, and if is blank, assign it a blank or default value.

When in doubt..

Code: Select all

$var = (!empty($var) ? $var : 'defaultvalue');
When it comes down to it all your variables should have expected values, and you should be constantly expecting a specific value(s).

It is not neccesarly a horrible thing to have undefined variables, after all they are just notices, but in the long run your going to run into a lot of unexpected problems if you continue down the path you are, especially on a larger application scale.

Posted: Mon Mar 07, 2005 9:07 am
by McGruff
Best not to have any globals in the first place. That will force you into designing the application in such a way that everything is brought into clearer focus. Or at least it will after a year or two studying OOP and patterns.