php – How to separate categories with commas?

I have this function that shows the category of specified hierarchy level of a publication. For example will show the top category / parent, while will show the second category of child.

If the publication has more than one category in a given level, the categories will be displayed online without spaces or commas between them like this one. cat1acat1bcat1c instead of cat1a, cat1b, cat1c. How would you update the function to separate the multiple categories shown by commas?

get_level function ($ category, $ level = 0)
{
if ($ category-> parent == 0) {
returns $ level;
} else {
$ level ++;
$ category = get_term ($ category-> parent);
returns get_level ($ category, $ level);
}
}

function display_cat_level ($ level = 0, $ link = false) {

$ cats = get_the_terms (null, & # 39; category & # 39;);
yes ($ cats) {
foreach ($ cats like $ cat) {
$ current_cat_level = get_level ($ cat);
if ($ current_cat_level == $ level) {
if ($ link == true) {
eco & # 39;