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