'Get two acf fields comma separated

I want to show two acf fields comma separated in single line if both are available, otherwise show one field which is available without comma.

I got the code, but not sure where to put <?php code to make it work.

if (get_field('birthDate') {
  $values[] = get_field('birthDate');
}
if (get_field('birthPlace') {
  $values[] = get_field('birthPlace');
}
if (!empty($values)) {
  echo implode(', ', $values);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source