1

The company keeps their PHP errors suppressed in code, but I turned it on to see why something of mine was not working.

Now, however, I am getting hundreds of Undefined index messages that I would like to turn off so that I can find the messages from MY code.

Here is one particular block that gives many errors:

final public function getAttribute($name) {
  $value = '';
  if(is_array($this->attributes[$name]) === false) { // Notice: Undefined index: name
    $value = trim($this->attributes[$name]);
  } else {
    $value = trim(implode(' ', $this->attributes[$name]));
  }
  return $value;    
}

To eliminate these notices, I followed the post Why check both isset() and !empty() to write it like this:

final public function getAttribute($name='') {
  if (!isset($name) || empty($name)) {
    return '';
  }
  $value = '';
  if(is_array($this->attributes[$name]) === false) { // Notice: Undefined index: name
    $value = trim($this->attributes[$name]);
  } else {
    $value = trim(implode(' ', $this->attributes[$name]));
  }
  return $value;    
}

I still get the notices, and in the same place.

How do I fix the code so that it does not create this condition?

Community
  • 1
  • 1

5 Answers5

6

You are not using the right variable to check against. You need to check for whether the index of the array exists.

final public function getAttribute($name='') {
  if (!isset($this->attributes[$name]) || empty($this->attributes[$name])) {
    return '';
  }
  // ... 
}
Demonslay335
  • 776
  • 7
  • 17
2

try isset($this->attributes[$name]) in your second block

Ethan Fang
  • 1,271
  • 10
  • 15
1

This will not throw undefined index:

[...]
if (isset($this->attributes[$name])) {
    // The key $name is set in $this->attributes
}
else {
    // The key is not set
}
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0
if (!isset($this->attributes[$name])) {
  return NULL;
}

The above code should do it.

raj
  • 819
  • 5
  • 9
-1

Use empty first, because isset will throw undefined error if the index doesn't exist, and empty will never do, like:

if (empty($name) && !isset($name)) {
    return '';
}

Using AND here will prevent returning if the value is already set but equals to '', o or false.

If you want to return if the value is true, some string or value, just use empty. Not need to use isset.

jvicab
  • 266
  • 1
  • 3
  • I believe your statement about `isset()` throwing the error is incorrect. The point of `isset()` is to check if a variable is defined; it seems against its design to throw an error on that. `empty` won't either. Either way, this was not what attributes to his issue, as it is not a concern about the `$name` variable being set, but rather the index key that it represents. – Demonslay335 Jan 20 '14 at 23:00
  • 1
    Yes, but if isset is used to test an associative array with an unknow key, you will get the error, which will be prevented for happening if use empty instead – jvicab Jan 21 '14 at 12:12
  • `isset()` will not throw an error, it is designed to check if the variable exists, which in this case would be the key of the array. I've tested with the following code, and only get an E_NOTICE on the last (without `empty()` or `isset()`): `error_reporting(E_ALL | E_STRICT); $arr = array('foo' => 'bar'); var_dump(isset($arr['baz'])); var_dump(empty($arr['baz'])) var_dump($arr['baz']);` However, upon inspecting the manual for both, I do see `empty()` alone is more suited for this type of situation, as it essentially does the same as `isset()` internally; I've learned something. – Demonslay335 Jan 22 '14 at 22:12