title: remove last comma from string in while loop in php?
body: <p>I am using while loop to get values from my database and my result is like:</p>
  
  <p><a href="https://i.stack.imgur.com/SYT0F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SYT0F.png" alt="enter image description here"></a></p>
  
  <p>I want to remove comma after last value of loop.</p>
  
  <p>i used code like..</p>
  
  <pre><code>&lt;?php
   $cat = get_terms('car_category'); // you can put your custom taxonomy name as place of category.
          foreach ($cat as $catVal) {
              echo '&lt;b&gt;'.$catVal-&gt;name.'&lt;/b&gt;';
              $postArg = array('post_type'=&gt;'cars','posts_per_page'=&gt;5,'order'=&gt;'desc',
                                'tax_query' =&gt; array(
                                                      array(
                                                          'taxonomy' =&gt; 'car_category',
                                                          'field' =&gt; 'term_id',
                                                          'terms' =&gt; $catVal-&gt;term_id
                                                      )
                              ));
  
              $getPost = new wp_query($postArg);
              global $post;
  
              if($getPost-&gt;have_posts()){
                  $str = "";
                  echo '&lt;div class="yearDiv"&gt;';
                      while ( $getPost-&gt;have_posts()):$getPost-&gt;the_post();
                          $str =rtrim($post-&gt;post_title, ",");
                          echo '&lt;span&gt;'.$str.',&lt;/span&gt;';
                      endwhile;
                  echo '&lt;/div&gt;';
              }
          }
  ?&gt;
  </code></pre>
  
  <p>i used rtrim , substr function also for it but comma not removing.</p>