How to find out if option exists but is empty?

Basically to distinguish between false boolean value and '' empty string you must use more strict comparison operator.

var_dump( '' == false ); // this is 'true', treated like two 'empty()' values

var_dump( '' === false ); // this is 'false', because values are both 'empty()' BUT of different type

But there is more. Since what you want is very typical – get_option() already can provide default value on its own. So your code can be simplified to:

$myOption = get_option( 'myOption', $myOption_def );

Note that this will correctly determine empty string and won’t apply default value in that case.

Leave a Comment