Why return an empty string instead of null when there is nothing in the field
-
Maybe a noob question but was causing some frustration.
Is there a reason to return an empty string instead of NULL when getting a fields when it doesn’t have a value?
I want to check if a field is null. i.e. if someone has forgotten to fill it out, if they have I want to run another some other code. Here is an example.
//uses Types Field API https://wp-types.com/documentation/functions/ //get our custom title.... $post_h1_title = types_render_field('h1-title', array('raw' => TRUE)); if (!is_null($post_h1_title)) { echo $post_h1_title; } //but if that doesn't exist then get the post title else { the_title(); }
The only problem is that this doesn’t work because types_render_field returns an empty string if the field isn’t filled out. I realize I could test for this, but that doesn’t seem to be intuitive.
So is there a reason for returning an empty string instead of something that can be tested with isset or is_null? If there is that is cool, if not then could this get changed?
Thanks for your work on the plugin and for the great documentation. Cheers!
- The topic ‘Why return an empty string instead of null when there is nothing in the field’ is closed to new replies.