略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Generator::current

2024-04-29

Generator::current

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

Generator::current返回当前产生的值

说明

public Generator::current(): mixed

参数

此函数没有参数。

返回值

返回当前产生的值。

add a noteadd a note

User Contributed Notes 2 notes

up
0
gib-o-master
7 months ago
current() advances untouched generator, same as next(), it makes the first step/iteration. the following calls will not.

non-yielded value will be NULL
up
-1
mrsoftware73 at gmail dot com
4 years ago
<?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
"regular : ".$value , PHP_EOL;
    echo
"With current function : ".$generator->current(),PHP_EOL;
}
?>

官方地址:https://www.php.net/manual/en/generator.current.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3