0

I am trying to learn range and xrange functionality by plugging PHP.net code into a code generator. When I run the following code I am given the error:

unexpected '$i' (T_VARIABLE) on line 4

Here is the code:

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


foreach (range(1, 9, 2) as $number) {
    echo "$number ";
}

Thanks in advnace for your insight!

Willow
  • 1,040
  • 2
  • 10
  • 21
  • 4
    Be sure you are working with `PHP 5.5`. – moonwave99 May 21 '13 at 16:25
  • The generator works with PHP 5. Is this the cause for the error? Though PHP.net states: (PHP 5 >= 5.5.0) http://www.php.net/manual/en/language.generators.overview.php – Willow May 21 '13 at 16:29

1 Answers1

0

From Generators doc:

(PHP 5 >= 5.5.0)

So it won't work with PHP 5.4 or below.

If you want to try PHP 5.5 online, use codepad.viper-7.

moonwave99
  • 21,957
  • 3
  • 43
  • 64