Avatar ·

How to remove the last separator at the end of a string in PHP?

Hello everyone! I'm a beginner PHP developer and I've encountered a problem when forming a string from an array of data. I need to join array elements with a comma, but there's always an extra separator at the end of the string. Here's an example of my code: ```php $fruits = ['apple', 'banana', 'orange']; $result = ''; foreach ($fruits as $fruit) { $result .= $fruit . ', '; } echo $result; // Outputs: apple, banana, orange, ``` As you can see, after the last element `orange` there's a comma with a space. This looks ugly and can cause problems when further processing the string, especially if I'm saving it to a CSV file or passing it to an API. I've tried using the `implode()` function, but it adds the separator only between elements, not after each one. This works, but I'm curious