Empty Needle error?

Not sure why I am getting an “Empty needle in…” warning for Line 12 of the following code:

1  function check_usstate($usstate, $optional)
2  {
3   $list_usstates = '|AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC'
4   				. '|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS'
5   				. '|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO'
6   				. '|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP'
7   				. '|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN'
8   				. '|TX|UT|VT|VI|VA|WA|WV|WI|WY|';
9  
10   if ( (strlen($usstate) == 0) && ($optional === true) ) {
11    return true;
12   } elseif (strpos($list_usstates, strtoupper($usstate)) === false ) {
13    return false;
14   } else {
15    return true;
16   }
17  }

Any help, thoughts or comments are appreciated. Thanks!

if $optional is not identical to true then $usstate may be an empty string, hence the error.

1 Like

Thank you!

As one of my “rule of thumb” habits I trim() input values in case there is leading or trailing spaces / tabs. And before I test a value I test to see it is !empty().

2 Likes

Great advice, thank you. :slight_smile:

Just remember that 0 and '0' are also considered empty.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.