Undefined index exceptions

angeliti@drupal.org's picture
public
angeliti@drupal.org - Thu, 2008-05-15 09:55

Hello everyone!

I keep getting 'Undefined index' exceptions in my tests:

Unexpected PHP error [Undefined index: weight] severity [E_NOTICE] in [/Applications/MAMP/htdocs/drupal-5.7/modules/taxonomy/taxonomy.module line 502]

As you all know, they occur in this kind of situations:

$example = array('name' => 'example');
if ($example['weight']) {
  ...
}

One option to avoid this is to check if the variable is set:

if (isset($example['weight'])) {
  ...
}

However, this is not done in the Drupal core code, so we will keep getting the exceptions when using functions like 'save_node' or 'taxonomy_save_term'.

The other option is to define EVERY index in our arrays, and just leave empty the ones we are not going to use. This is annoying and makes the code less legible.

What is the right way to proceed to avoid these exceptions?

Thank you!
Ángel.-


I'd say file a core patch...

webchick's picture
webchick - Thu, 2008-05-29 00:33

And have taxonomy.module instantiate itself with default values for those kinds of things. isset() in 20,000 places sounds like a nightmare.

Thanks for looking into this!!