Bug->Doc #77489 [Opn->Ver]: Yield inside IF instruction
| From: | cmb@php.net | Date: | Sat, 19 Jan 2019 11:42:41 +0000 |
| Subject: | Bug->Doc #77489 [Opn->Ver]: Yield inside IF instruction | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16349@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77489&edit=1
ID: 77489
Updated by: cmb@php.net
Reported by: hlecorche at e-commit dot fr
Summary: Yield inside IF instruction
-Status: Open
+Status: Verified
-Type: Bug
+Type: Documentation Problem
Package: Scripting Engine problem
Operating System: Debian GNU/Linux
PHP Version: 7.2.14
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[2019-01-19 10:54:10] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77489&edit=1