Skip to content

Strange generator behaviour #17244

@CiBeRHeMuL

Description

@CiBeRHeMuL

Description

Hello!

There is so strange behavior of the code when using the generator inside the if branch.

The following code:

<?php

$condition = false;
$func = fn() => $condition ? yield from [] : new \ArrayIterator([1, 2, 3]);
$iterator = $func();
foreach ($iterator as $el) {
    echo "$el\n";
}

Don't print any to the output.
It seems like yield clause is executing anytime despite the code execution (code in example will never enter the if branch).

So, this code should produce this output:

1
2
3

PHP Version

PHP 8.3.14

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions