Doc #77489 [Ver->Csd]: Yield inside IF instruction

From: Date: Mon, 21 Jan 2019 10:51:11 +0000
Subject: Doc #77489 [Ver->Csd]: Yield inside IF instruction
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-16357@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: Verified +Status: Closed Type: Documentation Problem Package: Scripting Engine problem Operating System: Debian GNU/Linux PHP Version: 7.2.14 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2019-01-21 10:50:39] 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-19 11:42:41] 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-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

« previous php.doc.bugs (#16357) next »