0

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 ??

3
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by twiss
0
<?php
    for ($a=1; $a<=10; $a++)
    {
    if($a!=10)
        echo $a . ", ";
    else
        echo $a.".";
    }
?>

Edited by karthik_ppts: n/a

0

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.".";
    }
?>
0

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.".";
    }
    ?>
0
<?php
$arr=new array();
$n=10;
for ($a=1; $a<=$n; $a++)
{
    $arr[$a-1]=$a;
}
$val=implode(",",$arr);
echo $val.".";
?>
0

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

Edited by twiss: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.