I have a string called $columns
which dynamically gets a value from 1 to 7. I want to create a loop of
for however many times
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
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>';
}
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}
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>";
}
If you just need to use number of repeat count:
for ($i = 0; $i < 5; $i++){
// code to repeat here
}
A for loop will work:
for ($i = 0; $i < $columns; $i++) {
...
}