Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top