I made the first form following the example put in the original question, repeating the value. I doubted it was this, but I answered it anyway. There are simpler ways to do this.
The second is the way to do what you really want, according to the comments and issue of the question. And the third is the simplified way to get the same result.
//solução baseada na pergunta original
$tam = 4;
$minhastring = '';
$valor = 'teste';
for ($i = 0; $i < $tam; $i++) $minhastring .= $valor . ',';
$minhastring = substr($minhastring, 0, -1);
echo $minhastring . "\n";
//solução para a pergunta real
$minhastring = '';
$valor = Array('teste0', 'teste1', 'teste2', 'teste3');
$tam = count($valor);
for ($i = 0; $i < $tam; $i++) $minhastring .= $valor[$i] . ',';
$minhastring = substr($minhastring, 0, strlen($minhastring) - 1);
echo $minhastring . "\n";
//a mesma solução com função pronta - recomendada
$minhastring = implode($valor, ",");
echo $minhastring;
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
You want to repeat
$valor
a number of times? Or that variable is wrong there?– Maniero
Yes, actually this variable $value is dynamic, because it comes from an array...here is $value[$c]
– Charles Fay
every time you enter the loop its value is different....
– Charles Fay