php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77489 Yield inside IF instruction
Submitted: 2019-01-19 10:54 UTC Modified: 2019-01-21 10:51 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: hlecorche at e-commit dot fr Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.2.14 OS: Debian GNU/Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hlecorche at e-commit dot fr
New email:
PHP Version: OS:

 

 [2019-01-19 10:54 UTC] hlecorche at e-commit dot fr
Description:
------------
When a function has a YIELD instruction inside a IF instruction, the yield instruction is called even if the IF INSTRUCTION is not true.

Tested whith 7.0.33, 7.1.26 and 7.2.14 versions.

Test script:
---------------
function gen_one_to_three($useYield = true)
{
    $result = [];
    for ($i = 1; $i <= 3; $i++) {
        if (true === $useYield) {
            yield $i;
        } else {
            $result[] = $i;
        }
    }

    if (false === $useYield) {
        return $result;
    }
}

$return1 = gen_one_to_three(true);
echo get_class($return1)
$return2 = gen_one_to_three(false);
echo get_class($return2);

Expected result:
----------------
Generator
PHP Warning:  get_class() expects parameter 1 to be object, array given

Actual result:
--------------
Generator
Generator

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-19 11:42 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2019-01-19 11:42 UTC] cmb@php.net
The language specification clarifies this[1]:

| Any function containing yield is a generator function.

The PHP manual could be improved in this regard, though.

[1] <https://github.com/php/php-langspec/blob/master/spec/13-functions.md#general>
 [2019-01-21 10:50 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=346612
Log: Fix #77489: Yield inside IF instruction
 [2019-01-21 10:51 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-01-21 10:51 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:05 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=3faf6fd5c8aabe93627578d06d7bbf343cedb3b8
Log: Fix #77489: Yield inside IF instruction
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 23:01:30 2024 UTC