Assuming $postcode
comes from either a $POST
or $GET
, it always is a string. !is_null()
will, therefore be FALSE, regardless:
php> var_dump(is_null(""))
#=> bool(false)
You could revert to using empty()
, which is more liberal. However, PHP is utterly inconsistent and weird when it comes to these checks. For example, empty()
will return FALSE
for 0 too. Yup.
php> $postcode = "";
php> var_dump(empty($postcode))
#=> bool(true)
php> $postcode = 0;
php>var_dump(empty($postcode))
#=> bool(true)
A much better approach, is to do some sort of "duck-typing". In your case: when it can be converted to a numeric value, do that and use it. Then leave it to the language to determine what it considers "number-ish" enough to convert.
php> var_dump((int) "")
int(0)
php> var_dump((int) "13")
int(13)
So:
else if(($postcode = (int) $postcode) && ($postcode > 0)) {
}
And last, off-topic: a heed of warning about your business assumptions: postcodes are not always numeric. Yes, in the US most are. But there are more countries out there (saying this as a EU-citizen who comes way too often about sites that assume everyone is an average-US-citizen)