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

In python we have:

for i in range(length) 

What about PHP?

+6
python php range
source share
5 answers

Straight from docs:

 foreach (range(0, 12) as $number) { echo $number; } 
+9
source share

Old fashioned for loops:

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

Or use foreach with a function :

 foreach (range(1, 10) as $i) { // ... } 
+4
source share

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

or

foreach (range(0, LENGTH_GOES_HERE - 1) as $i) { ... } , cf. range () .

+1
source share

There is a range function in php, you can use this.

 foreach( range(0,10) as $y){ //do something } 

but unlike python, you have to pass 2 parameters, range (10) will not work.

+1
source share

Try the following:

 // Generates the digits in base 10. // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) foreach (range(0, 9) as $number) { echo $number; } 
+1
source share

All Articles