I was trying to get yield working and I copied and pasted the following code from http://php.net/manual/en/language.generators.syntax.php into an empty file and got the error Parse error: syntax error, unexpected '$i' (T_VARIABLE) in [FILENAME]
I'm running XAMPP v3.2.1 which has been working perfectly for the rest of my code (haven't used a yield statement yet) and PHP 5.4.16.
Any idea what I'm doing wrong or what I should do?
<?php
function gen_one_to_three() {
for ($i = 1; $i <= 3; $i++) {
// Note that $i is preserved between yields.
yield $i;
}
}
$generator = gen_one_to_three();
foreach ($generator as $value) {
echo "$value\n";
}
?>
the code has no error if you replace yield with echo
yield
is available only on PHP versions > 5.5
.
If you try to use it on previous versions, you'll get a T_VARIABLE
parse error.
See 3v4l demo.