?
Solved

Notice: Undefined index: in

Posted on 2011-04-28
10
Medium Priority
?
382 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

0
Comment
Question by:rgb192
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35487617
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
 
LVL 1

Author Comment

by:rgb192
ID: 35487643
code change works some of the time
I do not know what the pattern is
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35487651
When does it work and when not?
What kind of errors do you get?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:rgb192
ID: 35487655
no errors
just same warning
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35487676
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
 
LVL 82

Accepted Solution

by:
hielo earned 1400 total points
ID: 35487797
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35487889
$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
 
LVL 1

Author Comment

by:rgb192
ID: 35494024
return isset($states[$state]) ? $states[$state]: '';

works

I dont understand what it means

what is the purpose of having
: ''

at the end
 
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 600 total points
ID: 35494299
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
 
LVL 1

Author Closing Comment

by:rgb192
ID: 35494565
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question