Re: Mystery foreach error
| From: | Angela Barone | Date: | Tue, 12 Mar 2013 22:57:33 +0000 |
| Subject: | Re: Mystery foreach error | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320502@lists.php.net to get a copy of this message | ||
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