Re: [PHP] Mystery foreach error
On Mar 13, 2013, at 5:02 PM, David Harkness wrote: > isset() will return false for an array key 'foo' mapped to a null value > whereas array_key_exists() will return true. The latter asks "Is this key in > the array?" whereas isset() adds "and is its value not null?" While isset() > is every-so-slightly faster, this should not be a concern. Use whichever > makes sense for the context here. Hi David, Thank you for the explanation. It's nice to know the difference between them. Since they are equivalent for my use, I went with array_key_exists, simply because it makes more sense to me in English. ;) Thanks again to everyone. I got it to work _and_ there are no more errors!!! Angela -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mystery foreach error
2013/3/14 David Harkness > > On Wed, Mar 13, 2013 at 5:10 PM, Sebastian Krebs wrote: > >> Because 'null' is the representation of "nothing" array_key_exists() and >> isset() can be treated as semantically equivalent. > > > As I said, these functions return different results for null values. It > won't matter for Angela since she isn't storing null in the array, though. > Thats exactly, what I tried to say :) > > Peace, > David > > -- github.com/KingCrunch
Re: [PHP] Mystery foreach error
On Wed, Mar 13, 2013 at 5:10 PM, Sebastian Krebs wrote: > Because 'null' is the representation of "nothing" array_key_exists() and > isset() can be treated as semantically equivalent. As I said, these functions return different results for null values. It won't matter for Angela since she isn't storing null in the array, though. Peace, David
Re: [PHP] Mystery foreach error
2013/3/14 David Harkness > On Wed, Mar 13, 2013 at 4:44 PM, Angela Barone > wrote: > > > I ran across if(array_key_exists) and it seems to work. How does that > > differ from if(isset($states[$state]))? > > > Hi Angela, > > isset() will return false for an array key 'foo' mapped to a null value > whereas array_key_exists() will return true. The latter asks "Is this key > in the array?" whereas isset() adds "and is its value not null?" While > isset() is every-so-slightly faster, this should not be a concern. Use > whichever makes sense for the context here. Since you don't stick null > values into the array, I prefer the isset() form because the syntax reads > better to me. > Just a minor addition: Because 'null' is the representation of "nothing" array_key_exists() and isset() can be treated as semantically equivalent. Another approach (in my eyes the cleaner one ;)) is to simply _ensure_ that the keys I want to use exists. Of course this only works in cases, where the key is not dynamical, or the dynamic keys are known, which is not the case here, it seems. $defaults = array('stateNames' => array(), 'states' => array()); $values = array_merge($defaults, $values); $values['states'] = array_merge(array_fill_keys($values['stateNames'], null), $values['states']); if (!$values[$myState]) { } > > Peace, > David > -- github.com/KingCrunch
Re: [PHP] Mystery foreach error
On Wed, Mar 13, 2013 at 4:44 PM, Angela Barone wrote: > I ran across if(array_key_exists) and it seems to work. How does that > differ from if(isset($states[$state]))? Hi Angela, isset() will return false for an array key 'foo' mapped to a null value whereas array_key_exists() will return true. The latter asks "Is this key in the array?" whereas isset() adds "and is its value not null?" While isset() is every-so-slightly faster, this should not be a concern. Use whichever makes sense for the context here. Since you don't stick null values into the array, I prefer the isset() form because the syntax reads better to me. Peace, David
Re: [PHP] Mystery foreach error
On Mar 13, 2013, at 4:24 PM, Matijn Woudt wrote: > That wouldn't work, in_array checks the values, and your states are in the > keys. Use: > if(isset($states[$state])) Hi Matijn, Before I received your email, I ran across if(array_key_exists) and it seems to work. How does that differ from if(isset($states[$state]))? Angela -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mystery foreach error
On Thu, Mar 14, 2013 at 12:18 AM, Angela Barone wrote: > On Mar 13, 2013, at 9:07 AM, Jim Giner wrote: > > Why not just check if the $state exists as a key of the array $states > before doing this? > > Jim, > > Are you thinking about the in_array function? > > Angela That wouldn't work, in_array checks the values, and your states are in the keys. Use: if(isset($states[$state])) - Matijn
Re: [PHP] Mystery foreach error
On Mar 13, 2013, at 9:07 AM, Jim Giner wrote: > Why not just check if the $state exists as a key of the array $states before > doing this? Jim, Are you thinking about the in_array function? Angela
Re: [PHP] Mystery foreach error
On Wed, Mar 13, 2013 at 5:07 PM, Jim Giner wrote: > On 3/12/2013 9:04 PM, Angela Barone wrote: > >> On Mar 12, 2013, at 5:16 PM, David Robley wrote: >> >>> Presumably there is a fixed list of State - those are US states? - >>> >> >> so why not provide a drop down list of the possible choices? >>> >> >> There is, but the problem must have been that if someone didn't >> select a State, $state was blank. I've since given the "Select a State..." >> choice a value of 'XX' and I'm now looking for that in the if statement I >> mentioned before. >> >> Angela >> >> Why not just check if the $state exists as a key of the array $states > before doing this? Exactly, that's much better. It could be that some hacker enters something other than XX or one of the states..
Re: [PHP] Mystery foreach error
On 3/12/2013 9:04 PM, Angela Barone wrote: On Mar 12, 2013, at 5:16 PM, David Robley wrote: Presumably there is a fixed list of State - those are US states? - so why not provide a drop down list of the possible choices? There is, but the problem must have been that if someone didn't select a State, $state was blank. I've since given the "Select a State..." choice a value of 'XX' and I'm now looking for that in the if statement I mentioned before. Angela Why not just check if the $state exists as a key of the array $states before doing this? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mystery foreach error
On Mar 12, 2013, at 5:16 PM, David Robley wrote: > Presumably there is a fixed list of State - those are US states? - > so why not provide a drop down list of the possible choices? There is, but the problem must have been that if someone didn't select a State, $state was blank. I've since given the "Select a State..." choice a value of 'XX' and I'm now looking for that in the if statement I mentioned before. Angela
Re: [PHP] Mystery foreach error
Angela Barone wrote: > I think I figured it out. > > $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
Re: [PHP] Mystery foreach error
I think I figured it out. 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mystery foreach error
$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', ), ); ?> Seeing your structure now, I think the problem is that '$state' is not set to a value in the array. Are you picking a State and assigning it to $state before executing this loop? If not, then $states[$state] is undefined which is not an array var. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mystery foreach error
On Mar 12, 2013, at 2:26 PM, Marco Behnke wrote: > what is in $states? > Looks like $states[$state] is not an array. Here's a sample: 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', ), ); ?> > side effects if you don't act carefully with it. I don't remember where that came from, but for the most part, this script works perfectly. However, I removed it and will test without it. Angela -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mystery foreach error
Am 12.03.13 20:45, schrieb Angela Barone: > I've been getting the following error for awhile now, but I can't > figure out why it's happening: > > Invalid argument supplied for foreach() in ... sample.php on line 377 > > Here's that portion of code: > > include("states_zipcodes.php"); > > // Check if Zip Code matches from states_zipcodes > $zip_short = substr($zip, 0, 3); > foreach ($states[$state] as &$zip_prefix) { // <-- line 377 what is in $states? Looks like $states[$state] is not an array. And don't use & reference operator in foreach loop, there can be strange side effects if you don't act carefully with it. > if ($zip_prefix == $zip_short) { > break; > } else { > $match = 'no'; > } > } > > It doesn't happen all the time, so I'm thinking that some spambot is > populating the HTML form with something the script doesn't like(?). However, > I tested that myself by entering text, or by entering just 2 digits, but > there was no error. FYI, I do have code in the script that catches faulty > input and warns people in their browser to go back and re-enter the correct > data, so I'm at a loss as to why this is happening. > > How can I see what's triggering this to happen? I have the following > line in my php.ini: > > error_reporting = E_ALL & E_STRICT & E_NOTICE & E_DEPRECATED > > Thank you, > Angela -- Marco Behnke Dipl. Informatiker (FH), SAE Audio Engineer Diploma Zend Certified Engineer PHP 5.3 Tel.: 0174 / 9722336 e-Mail: ma...@behnke.biz Softwaretechnik Behnke Heinrich-Heine-Str. 7D 21218 Seevetal http://www.behnke.biz signature.asc Description: OpenPGP digital signature