Conditional display for custom fields/taxonomy

the_terms() echoes output. To assign output to a variable and check if there is any you need to use get_the_term_list() instead.

Note that it might also return WP_Error object (which won’t be empty) so you will need to check for it as well with is_wp_error().