iarp Posted October 12, 2011 Share Posted October 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/ Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2011 Share Posted October 12, 2011 Every error/warning/notice generated slows the script down. Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1278793 Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1278795 Share on other sites More sharing options...
iarp Posted October 12, 2011 Author Share Posted October 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1278796 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2011 Share Posted October 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1278807 Share on other sites More sharing options...
iarp Posted October 16, 2011 Author Share Posted October 16, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1279701 Share on other sites More sharing options...
PFMaBiSmAd Posted October 16, 2011 Share Posted October 16, 2011 $html .= (isset($v['uri']) && $this->getVar($v['uri'])) ? urldecode($_GET[$v['uri']]) : NULL; Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1279734 Share on other sites More sharing options...
teynon Posted October 16, 2011 Share Posted October 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1279744 Share on other sites More sharing options...
Psycho Posted October 16, 2011 Share Posted October 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249000-should-i-be-worried-about-undefined-indexs/#findComment-1279792 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.