Beauford2016 Posted October 7, 2020 Share Posted October 7, 2020 I don't know who did this or why this was implemented, but it was the worst decision PHP made as it causes nothing but problems. Not having to define variables was one of the reasons I liked PHP. This is just my opinion so no roasts. Question is, what is the proper way to do this so I don't get this message 'cause I am hit and miss, even when I think I have them defined this stupid message pops up. This is the 3rd line on the on script that is causing me grief at the moment: $submit = $populate = $record = $row[] = $hid = $uid = $asset_tag = $type = $manufacturer = $model = $serial_number = $status = $location = $firstname = $lastname = $go = ""; Not sure why I need to have $row[] as it is the result of a SELECT and values are being given to it. Also not sure why I need it for $submit or $_POST as they already have values when they are passed to this page, so how can they be undefined when they have values. Just not getting it. Any help with this would really be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/ Share on other sites More sharing options...
benanamen Posted October 7, 2020 Share Posted October 7, 2020 (edited) 8 minutes ago, Beauford2016 said: $submit = $populate = $record = $row[] = $hid = $uid = $asset_tag = $type = $manufacturer = $model = $serial_number = $status = $location = $firstname = $lastname = $go = ""; Who/what/where in the world taught you to do this? Just stop it! Stop it right now! Post code that shows what you are actually trying to do. Edited October 7, 2020 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/#findComment-1581754 Share on other sites More sharing options...
Barand Posted October 7, 2020 Share Posted October 7, 2020 (edited) I assume this post is just so you can have a rant. If it were a serious request for help then you should have provided some information about the problem, the error message you are receiving and the code that is giving error Then, perhaps, someone can help. BTW, coincidentally, I used the same syntax myself earlier today (though not to that extreme without any regard for where and how the variable are to be used, and I am not happy about that $row[] in there) when setting default input values for a form Edited October 7, 2020 by Barand replace iage Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/#findComment-1581756 Share on other sites More sharing options...
Beauford2016 Posted October 7, 2020 Author Share Posted October 7, 2020 I thought the question was well laid out myself. The reason I asked it this way is it is every piece of code I write where a variable is concerned I have to fight with this error. I can post every script I have written if you want to sift through them all, but I am asking in general how do I write ANY code so as not to get these errors. I thought defining $variable = "" like this at the beginning of my page would solve this, but in many cases not. If somewhere down the code I use $variable = "Whatever", boom, undefined variable $variable - or in other cases undefined index depending on the variable. If this isn't enough for anyone to help, not a problem, I'll move on. Cheers, benanamen: chill man, you gonna bust an artery. Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/#findComment-1581758 Share on other sites More sharing options...
Barand Posted October 7, 2020 Share Posted October 7, 2020 36 minutes ago, Beauford2016 said: I am asking in general how do I write ANY code so as not to get these errors. "In general", don't write code which relies on a variable having been previously defined without first defining that variable or checking if the variable has been defined before you attempt to reference it. Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/#findComment-1581759 Share on other sites More sharing options...
kicken Posted October 7, 2020 Share Posted October 7, 2020 Undefined index (as specified in your title) refers specifically to array indexes, not variables in general so code like you posted wouldn't really help any. The common scenario where you encounter this is with the input arrays $_POST, $_GET, etc as what they contain depends on the request. Since you can't be certain whether a given key exists in those arrays, you should always be checking for it and providing a sensible fall-back value if it doesn't. With PHP 7 this has been made easy by using the null coalesce operator. In older versions you'd have to use isset() to check. There are other less common scenarios where the indexes of an array may or may not exist, but the results of a database query (implied in the question) generally isn't one of them. You should know what you're selecting and whatever you're selecting is what will be defined in your array. So the answer to your in general inquiry is that you generally should know whether or not a given index will be defined in an array. If it will 100% for sure be defined then you should not have an issue with this error. If you're unsure then you need to either find out, or code around the possibility that it doesn't exist by using either the null coalesce operator or isset(). One thing I do to handle this with regard to $_POST is define exactly what indexes I expect to exist and initialize them to null using array_fill_keys, then I replace the null values with whatever was in $_POST. Then I know the keys I am interested in will exist, but might be null. $data = array_fill_keys(['first','last','email'], null); $data = array_replace($data, array_intersect_key($_POST, $data)); // Now $data['first'], $data['last'], and $data['email'] are guaranteed to exist. Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/#findComment-1581760 Share on other sites More sharing options...
Beauford2016 Posted October 10, 2020 Author Share Posted October 10, 2020 Thanks for the reply kicken I'll play around with that and see how it works out. Quote Link to comment https://forums.phpfreaks.com/topic/311574-notice-undefined-index-driving-me-insane/#findComment-1581810 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.