Here's a Drupal 8/9/+ answer, designed as a static PHP class method:
/**
* Utility: find term by name and vid.
*
* @param string $name
* Term name.
* @param string $vid
* Term vid.
*
* @return int
* Term id, or 0 if none.
*/
public static function getTidByName($name = NULL, $vid = NULL) {
if (empty($name) || empty($vid)) {
return 0;
}
$properties = [
'name' => $name,
'vid' => $vid,
];
$terms = \Drupal::service('entity_type.manager')->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
return !empty($term) ? $term->id() : 0;
}