this is the simple php for loop,

<?php
for ($a=1; $a<=10; $a++)
	echo $a . ", ";
	?>

the output of this program is

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

i want to remove ',' from the last value
i want the output should be,

1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

the last comma should be replaced by a full stop, how can i do this ??

Recommended Answers

All 5 Replies

<?php
    for ($a=1; $a<=10; $a++)
    {
    if($a!=10)
        echo $a . ", ";
    else
        echo $a.".";
    }
?>

i know this but what if the last value of the loop is unknown to us ??

<?php
    for ($a=1; $a<=10; $a++)
    {
    if($a!=10)
        echo $a . ", ";
    else
        echo $a.".";
    }
?>

We should specify the end value of a loop.

<?php
    $n=10;
    for ($a=1; $a<=$n; $a++)
    {
    if($a!=$n)
    echo $a . ", ";
    else
    echo $a.".";
    }
    ?>
<?php
$arr=new array();
$n=10;
for ($a=1; $a<=$n; $a++)
{
    $arr[$a-1]=$a;
}
$val=implode(",",$arr);
echo $val.".";
?>

Yes. There's also count , which returns the amount of elements in an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.