How to make this code valid only when the value exists?

very new here to php and wordpress coding (1 day lol). This is my code so far, the problem is that when there is no value assigned to this attribute I receive and error message. I know I need something that says if the value exists then display the rest of the code but not sure how to go about adding that. This is for a woocommerce product attribute btw.

Code so far:

$  collection_values = get_the_terms( $  product->id, 'pa_collection');      foreach ($  collection_values as $  collection_value){         echo '<h2 class="collection_title"><a href="'.get_term_link($  collection_value->slug, $  collection_value->taxonomy).'">'.$  collection_value->name.'</a></h2>';     } 

Error message:

Warning: Invalid argument supplied for foreach() in /app/public/wp-content/themes/savoy/woocommerce/single-product/title.php on line 21 

EDIT: I am adding this above the product title in the title.php page.