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

Question:

In python we have:

for i in range(length)

What about in PHP?

Asked By: Mask

||

Answers:

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

or

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

Answered By: jensgram

Straight from the docs:

foreach (range(0, 12) as $number) {
    echo $number;
}
Answered By: int3

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.

Answered By: YOU

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;
}
Answered By: John Feminella

Old fashioned for loops:

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

Or foreach using the range function:

foreach (range(1, 10) as $i) {
    // ...
}
Answered By: Mikael S

Here’s a python compliant range generator


foreach ($x in xrange(10)) {
  echo "$x ";
} // expect result: 0 1 2 3 4 5 6 7 8 9

function xrange($start, $limit = null, $step = null) {
    if ($limit === null) {
        $limit = $start;
        $start = 0;
    }
    $step = $step ?? 1;
    if ($start <= $limit) {
        for ($i = $start; $i < $limit; $i += $step)
            yield $i;
    } else
        if ($step < 0) 
            for ($i = $start; $i > $limit; $i += $step)
                yield $i;
}

mostly cribbed from https://www.php.net/manual/en/language.generators.overview.php

Notes

  • It will not automatically step backwards (like python, unliike PHP)
  • It can accept 1, 2 or 3 arguments (like python)
  • It will count to $limit – 1 (like python, unlike PHP)
  • If your arguments are silly, you get no results (like python)
  • It doesn’t store the range in memory (unlike PHP) so you can have HUGE ranges
Answered By: Orwellophile
<?php
foreach(range(0,5) as $i) echo $i; // 012345
// but unlike python you also can range letters 
foreach(range('a','z') as $l) echo $l; //abcdefghijklmnopqrstuvwxyz
// or floating
foreach(range(0.1, 0.2, 0.01) as $f) echo "$f; "; //0.1; 0.11; 0.12; 0.13; 0.14; 0.15; 0.16; 0.17; 0.18; 0.19; 0.2;
// negative step work with letters
foreach(range('z','a',-1) as $l) echo $l; // zyxwvutsrqponmlkjihgfedcba
// and dont stuck if invalid negative step just ignore and use abs(step) value
foreach(range(1,10,-2) as $i) echo $i; // 13579
// but throw fatal if step exceed the specified range, so, test if step in range before using
foreach(range(1,10,-20) as $i) echo $i; 
// Fatal error: Uncaught ValueError: range(): Argument #3 ($step) must not exceed the specified range in /home/user/scripts/code.php:12
// Stack trace:
// #0 /home/user/scripts/code.php(12): range(1, 10, -20)
// #1 {main}
// thrown in /home/user/scripts/code.php on line 12
Answered By: vickrongo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.