title: Wordpress: How to add commas in this get_the_category function
body: <p><strong>PHP</strong></p>
  
  <pre><code>&lt;?php
       $categories = get_the_category();
       foreach ($categories as $category){
       echo $category-&gt;cat_name;}
  ?&gt;
  </code></pre>
  
  <p>Currently this will display categories as "cat1cat2cat3"
  What I want it to be is "cat1, cat2, cat3"</p>
  
  <p>I tried this <code>echo $category-&gt;cat_name . ', ';</code> 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, " </p>
  
  <p>So how can I get the commmas in but absent if just one category and absent on the last category if its a list?</p>