I have a string called $columns
which dynamically gets a value from 1 to 7. I want to create a loop of <td></td>
for however many times the value of $columns
is. Any idea how I can do this?

- 21,350
- 5
- 71
- 97

- 1,403
- 4
- 21
- 44
10 Answers
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}

- 4,015
- 8
- 43
- 70
-
5@tom - wouldn't `range()` be called first and the result of that is used in each iteration? – alexkb Jan 04 '19 at 02:55
-
2You are right @alexkb, range() will be evaluated only once. Source: https://stackoverflow.com/questions/1891917/does-using-a-function-in-foreach-loop-caches-the-result-or-calls-the-function-e – ecdani Aug 30 '19 at 12:31
-
2Be very careful here. `range(1, 0)` gets executed two times, which may not be what you're expecting. A normal for loop doesn't have that issue. – Pikamander2 Apr 28 '22 at 11:16
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }

- 4,319
- 3
- 33
- 45
-
I use this with the loop amount set to 2, but it occasionally returns 1 instead of 2. Is there a better way to limit the loop? I don't want it to occasionally loop once. It must loop twice at all times. (FYI, I've tried it with a difference amount of loops, and it still occasionally loops -1 of the intended loop amount). – Studocwho Jan 07 '18 at 22:17
If you just need to use number of repeat count:
for ($i = 0; $i < 5; $i++){
// code to repeat here
}

- 1,883
- 14
- 13
I like this way:
while( $i++ < $columns ) echo $i;
Just bear in mind if $columns
is 5, this will run 5 times (not 4).
Edit: There seems to be some confusion around the initial state of $i
here. You are welcome to initialise $i=0
beforehand if you wish. This is not required however as PHP is a very helpful engine and will do it for you automatically (tho, it will throw a notice if you happen to have those enabled).

- 6,482
- 9
- 52
- 80
-
You need to initialize $i with zero e.g. `$i = 0;`, so there will be one more line above it – vkovic Feb 22 '19 at 12:05
-
I admit that comment was posted before trying exact code you posted. Anyway, I just tried it, and got the error – vkovic Feb 26 '19 at 16:15
-
-
just repeat $n times? ... if dont mind that $n goes backwards... the advantage is that you can see/config "times" at the beginning
$n = 5;
while (--$n >= 0)
{
// do something, remember that $n goes backwards;
}

- 41
- 1
There is a str_repeat()
function in PHP, which repeats a string a number of times. The solution for your problem would be:
str_repeat( '<td></td>', $columns );

- 731
- 3
- 12
If $columns
is a string
you can cast to int
and use a simple for loop
for ($i=1; $i<(int)$columns; $i++) {
echo '<td></td>';
}

- 3,074
- 1
- 22
- 28
You can run it through a for loop easily to achieve this
$myData = array('val1', 'val2', ...);
for( $i = 0; $i < intval($columns); $i++)
{
echo "<td>" . $myData[$i] . "</td>";
}

- 14,484
- 3
- 48
- 77
Why use logic at all, don't waste those CPU cycles!
<td colspan="<?php echo $columns; ?>"></td>

- 3,935
- 2
- 30
- 44