Re: [PHP] Mystery foreach error

[email protected] (David Robley)
Newsgroups php.general
Message-ID <[email protected]>
Angela Barone wrote:

> I think I figured it out.
> 
> <?php
> $states = array(
>     'AL' => array( '350','351','352','353', ),
>     'AK' => array( '995','996','997','998','999', ),
>     'AZ' => array( '850','851','852','853','854', ),
>     'WI' => array( '530','531','532', ),
>     'WY' => array( '820','821','822','823','824', ),
> );
> 
> $zip = 35261;
> $state = 'XX';
> 
> $zip_short = substr($zip, 0, 3);
> foreach ($states[$state] as $zip_prefix) {
>     if ($zip_prefix == $zip_short) {
>         echo "State = $state";
>     } else {
>         echo 'no';
>     }
> }
> ?>
> 
> Running this script, I got the same error as before.  If $state is a known
> state abbreviation in the array, everything is fine, but if someone was to
> enter, say 'XX' like I did above or leave it blank, then the error is
> produced.  I placed an if statement around the foreach loop to test for
> that and I'll keep an eye on it.
> 
> Thank you for getting me to look at the array again, which led me to look
> at the State.
> 
> Angela

Presumably there is a fixed list of State - those are US states? - so why 
not provide a drop down list of the possible choices?

-- 
Cheers
David Robley

"I need to be careful not to add too much water," Tom said with great 
concentration.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.