Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like for i in range(length) in PHP?

Tags:

python

php

range

In python we have:

for i in range(length)

What about in PHP?

like image 781
Mask Avatar asked Nov 25 '09 14:11

Mask


2 Answers

Straight from the docs:

foreach (range(0, 12) as $number) {
    echo $number;
}
like image 125
int3 Avatar answered Oct 03 '22 11:10

int3


Old fashioned for loops:

for ($i = 0; $i < length; $i++) {
    // ...
}

Or foreach using the range function:

foreach (range(1, 10) as $i) {
    // ...
}
like image 31
Mikael S Avatar answered Oct 03 '22 10:10

Mikael S