Notice: Undefined index: in

return $states[$state];

generates


Notice: Undefined index: in
public function getState($state) {
    $states = array(
    'AA' => 'AA',
    'AP' => 'AP',
    'AE' => 'AE',
    'AK' => 'Alaska',
    'AL' => 'Alabama',
    'AR' => 'Arkansas',
    'WY' => 'Wyoming');
    return $states[$state];
  }


}

Open in new window

LVL 1
rgb192Asked:
Who is Participating?
 
hieloCommented:
public function getState($state) {
    $states = array(
    'AA' => 'AA',
    'AP' => 'AP',
    'AE' => 'AE',
    'AK' => 'Alaska',
    'AL' => 'Alabama',
    'AR' => 'Arkansas',
    'WY' => 'Wyoming');
     return isset($states[$state]) ? $states[$state]: '';
  }
0
 
Rik-LeggerCommented:
Thats because $state has no value,
you could do some extra validation:


public function getState($state) {
    $states = array(
    'AA' => 'AA',
    'AP' => 'AP',
    'AE' => 'AE',
    'AK' => 'Alaska',
    'AL' => 'Alabama',
    'AR' => 'Arkansas',
    'WY' => 'Wyoming');
    if ($state) {
        return $states[$state];
    } else {
        return false;
    }
  }


}

Open in new window

0
 
rgb192Author Commented:
code change works some of the time
I do not know what the pattern is
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Rik-LeggerCommented:
When does it work and when not?
What kind of errors do you get?
0
 
rgb192Author Commented:
no errors
just same warning
0
 
Rik-LeggerCommented:
Ah, i re-read your question,
it seems that your trying to get a state with the name 'in',
but no state with that key is available in your array.
0
 
Dave BaldwinFixer of ProblemsCommented:
$state as an array index will also be case sensitive.  If your keys are uppercase, you need to make sure $state is uppercased or it wont' return anything.

$state = strtoupper($state);
0
 
rgb192Author Commented:
return isset($states[$state]) ? $states[$state]: '';

works

I dont understand what it means

what is the purpose of having
: ''

at the end
 
0
 
Dave BaldwinFixer of ProblemsCommented:
It's part of the 'ternary operator': http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary   The two single quotes means return nothing (empty string).
0
 
rgb192Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.