We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Notice: Undefined index: in

rgb192
rgb192 asked
on
Medium Priority
394 Views
Last Modified: 2012-06-21
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

Comment
Watch Question

Top Expert 2011

Commented:
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

Author

Commented:
code change works some of the time
I do not know what the pattern is
Top Expert 2011

Commented:
When does it work and when not?
What kind of errors do you get?

Author

Commented:
no errors
just same warning
Top Expert 2011

Commented:
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.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
$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);

Author

Commented:
return isset($states[$state]) ? $states[$state]: '';

works

I dont understand what it means

what is the purpose of having
: ''

at the end
 
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.