21

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?

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
Ahmed
  • 1,403
  • 4
  • 21
  • 44

10 Answers10

45

Here's a more readable way to achieve this:

foreach(range(1,$columns) as $index) {
   //do your magic here
}
murze
  • 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
  • 2
    You 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
  • 2
    Be 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
44
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
Fatih Donmez
  • 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
12

If you just need to use number of repeat count:

for ($i = 0; $i < 5; $i++){
    // code to repeat here
}
Alex K - JESUS first
  • 1,883
  • 14
  • 13
4

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).

cronoklee
  • 6,482
  • 9
  • 52
  • 80
3

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;
}
atesin
  • 41
  • 1
3

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 );

V.Volkov
  • 731
  • 3
  • 12
2

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>';
}
Pankrates
  • 3,074
  • 1
  • 22
  • 28
1

A for loop will work:

for ($i = 0; $i < $columns; $i++) {
    ...
}
Blender
  • 289,723
  • 53
  • 439
  • 496
0

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>";
}
David Barker
  • 14,484
  • 3
  • 48
  • 77
-1

Why use logic at all, don't waste those CPU cycles!

<td colspan="<?php echo $columns; ?>"></td>
Kyle
  • 3,935
  • 2
  • 30
  • 44