What is the best way to determine if a value is 0 vs NULL vs Empty Set?
The code below is not doing what it needs to…
if (empty($number)){
// No Value.
What I meant to say was, “If the incoming variable is NULL or the Empty Set, then then is an error, otherwise keep going…”
Unfortunately, I was reminded - I always forget this! - that 0 is also “empty” as well!
Sincerely,
Debbie
To clarify something…
I am looking for an easy way to say…
“IF the input is equal to 0 or 0.0 or “0”, THEN do Thing-1, ELSE if it is NULL or Empty String or FALSE or anything that empty() normally covers then do Thing-2.”
Sincerely,
Debbie
You just need to evaluate the “if” clause further, i.e. first catch everything, then add another rule to avoid number 0
Example:
if (empty($number) && !is_numeric($number)) {}
or
if( is_numeric($number) && $number == 0 ) {
// 0 or 0.0 or "0" or "0.0"
} else if( $number === NULL ) {
// null
} else {
// something else
}