Jump to content

Notice: Undefined index: driving me insane


Recommended Posts

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.


Link to comment
Share on other sites

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 by benanamen
Link to comment
Share on other sites

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 by Barand
replace iage
Link to comment
Share on other sites

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.



benanamen: chill man, you gonna bust an artery.


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.


Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

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.