In python we have:
for i in range(length)
What about in PHP?
In python we have:
for i in range(length)
What about in PHP?
for ($i = 0; $i < LENGTH_GOES_HERE; $i++) { ... }
or
foreach (range(0, LENGTH_GOES_HERE - 1) as $i) { ... }
, cf. range().
Straight from the docs:
foreach (range(0, 12) as $number) {
echo $number;
}
There is a range function in php, you can use like this.
foreach( range(0,10) as $y){
//do something
}
but unlike python, you have to pass 2 parameters, range(10) will not work.
Try this:
// 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;
}
Old fashioned for
loops:
for ($i = 0; $i < length; $i++) {
// ...
}
Or foreach using the range function:
foreach (range(1, 10) as $i) {
// ...
}