Re: Mystery foreach error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux