In PHP, 0 == false == [] == '' == null
. A simple check to check if a variable or condition has a value will return false
if the value is equal to 0.
For 0
to return true as a valid value, you would need to make use of strict comparison by using the identical (===
) operator. Just remember, if you use ===
, not only the value must match, but the type as well, otherwise the condition will return false. If your values are a string, you should use '0'
, if they are integer values (which I doubt as custom field values are strings as single values), you should use 0
.
You can do the following check
$difficulty = get_field( 'difficulty' );
if ( $difficulty // Check if we have a valid value
|| '0' === $difficulty // This assumes '0' to be string, if integer, change to 0
) {
$value = $difficulty;
} else {
$value="n/a";
}
echo 'Difficulty: ' . $value;