Reputation: 1246
Here is my simple code logic just for the sake of simplicity. I want to concatenate string like: 10,11,12,13,14
, but it's just adding all the values
displaying wrong output as: ,100
it should display like: 10,11,12,13,14
like other programming languages do.
<?php
// list is a string variable
$list="";
//total number of seats in a Bus
$setnum=10;
for($i = 0; $i < 10; $i++) {
//i trying to concatenate string like 10,11,12,13,14
$list="$list".$i+$setnum.', ';
}
echo $list;
?>
Upvotes: 0
Views: 4160
Reputation: 21759
Try this:
// list is a string variable
$list = "";
// total number of seats in a Bus
$setnum = 10;
$list = "";
for($i = 0; $i < 10; $i++) {
// i trying to concatenate string like 10,11,12,13,14
$list .= ($i + $setnum) . ', ';
}
echo $list;
Upvotes: 1
Reputation: 5397
You can use implode to avoid having a comma at the end and also you can use range to render an array with numerical values between two points.
So you could have something like
$setnum=10;
echo implode(',', range($setnum, ($setnum+10)));
You can see this snippet in action here: http://codepad.org/4nrD0Ajk
Upvotes: 2
Reputation: 4133
The best way to concatenate strings is to add the strings to array and then implode
them. Read more about implode here
//total number of seats in a Bus
$setnum=10;
$list = array();
for($i = 0; $i < 10; $i++) {
//i trying to concatenate string like 10,11,12,13,14
$list[] = $i+$setnum;
}
echo implode(',', $list);
Upvotes: 3
Reputation: 776
Try this
// list is a string variable
$list = "";
// total number of seats in a Bus
$setnum = 10;
for($i = 0; $i < 10; $i++)
{
// i trying to concatenate string like 10,11,12,13,14
$list .= $i + $setnum . ',';
}
echo $list;
Upvotes: 1