PHP
<?php
$categories = get_the_category();
foreach ($categories as $category){
echo $category->cat_name;}
?>
Currently this will display categories as "cat1cat2cat3" What I want it to be is "cat1, cat2, cat3"
I tried this echo $category->cat_name . ', ';
but this just adds a comma after every category. Even if the post just has one category: Ex. "cat1, " And it also adds commas to the last category in the list: Ex. "cat1, cat2, cat3, "
So how can I get the commmas in but absent if just one category and absent on the last category if its a list?