PHP Question

PHP : How to trim last string inside the loop

Is there other way to trim this inside the loop? I have a loop and inside, i need to rtrim the last loop row, but what happen is it removes all character each of the last right string.

If I use this method, it works.

$plus = "";
for($i=0; $i<6; $i++){
$plus .= 'total'.$i."+";
}
echo rtrim($plus,'+');
//output
total0+total1+total2+total3+total4+total5


But how if i need to rtrim it inside the loop? Because I need for some reason

for($i=0; $i<6; $i++){
$plus = 'total'.$i."+";
echo rtrim($plus,'+');
}
//output says
total0total1total2total3total4total5

//This should be like
//total0+total1+total2+total3+total4+total5

Answer

That makes no sense. If you don't want the last + then rtrim() after the loop, why inside?

Just create an array and implode():

for($i=0; $i<6; $i++){
    $plus[] = 'total'.$i;

}
$plus = implode('+', $plus);