O
O
oc1tane2015-06-19 00:48:53
css
oc1tane, 2015-06-19 00:48:53

PHP loop - how to remove comma?

<?
echo "<h2>Вывести циклом 10 чисел</h2>";
echo " « ";
for ($i = 1; $i <= 10; $i++) {
   echo  "" . $i .", ";
}
echo "»";
?>

76a9aa4c1ad04f3e985fbd52c2edfd2b.png
how to remove the last comma?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Sergey, 2018-09-14
@sergey_st

Have a look at swiper , add some css and I think you can get what you need

N
Nazar Mokrinsky, 2015-06-19
@nazarpc

Why are you making it so complicated?)
"". - this is generally some kind of tin.

X
xmoonlight, 2015-06-19
@xmoonlight

was: it is
necessary:
​​-----------------------
In one line :)
UPD: but in fact the answer to the question was given by Igor Belka !

I
Igor Belikov, 2015-06-19
@igorbelikov

stackoverflow.com/questions/5592994/remove-the-las...
$str = substr($str, 0, -1);

S
Sergey Melnikov, 2015-06-19
@mlnkv

<?
echo "<h2>Вывести циклом 10 чисел</h2>";
$str = "";
for ($i = 1; $i <= 10; $i++) {
  $str .= ($str ? ", " : "") . $i;
}
echo "« $str »";
?>

Y
Yuri Mostovoy, 2015-06-19
@PROGRAMMATOR

$text = '';
    
    for ($i = 1; $i <= 10; $i++) {
      $text .= $i . ', ';
    }
    
    echo '«' . rtrim($text, ', ') . '»';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question