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?
PHP supports following four loop types.
We can print numbers from 1 to 10 by using for loop. You can easily extend this program to print any numbers from starting from any value and ending on any value. The echo command will print the value of $i to the screen. In the next line we have used the same echo command to print one html line break.
do... while - loops through a block of code once, and then repeats the loop as long as the specified condition is true. for - loops through a block of code a specified number of times. foreach - loops through a block of code for each element in an array.
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With