奇怪的PHP收益率错误 [英] Weird PHP Yield error

查看:102
本文介绍了奇怪的PHP收益率错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使产量提高,我从复制并粘贴了以下代码http://php.net/manual/zh-CN/language.generators.syntax.php 放入一个空文件,并出现错误解析错误:语法错误,[FILENAME]中出现意外的'$ i'(T_VARIABLE)

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]

我正在运行XAMPP v3.2.1,该代码对于我的其余代码(尚未使用yield语句)和PHP 5.4.16一直运行良好.

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";
}
?>

如果用echo替换yield,则代码没有错误

the code has no error if you replace yield with echo

推荐答案

yield仅在PHP版本5.5上可用.

yield is available only on PHP versions > 5.5.

如果尝试在以前的版本中使用它,则会出现T_VARIABLE解析错误.

If you try to use it on previous versions, you'll get a T_VARIABLE parse error.

请参见 3v4l演示.

这篇关于奇怪的PHP收益率错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆