Bug #72445 [Com]: while not considered a part of loop context when breaking from included file.

From: Date: Sat, 18 Jun 2016 14:05:18 +0000
Subject: Bug #72445 [Com]: while not considered a part of loop context when breaking from included file.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201716@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72445&edit=1 ID: 72445 Comment by: maikuolan at gmail dot com Reported by: maikuolan at gmail dot com Summary: while not considered a part of loop context when breaking from included file. Status: Not a bug Type: Bug Package: Unknown/Other Function Operating System: Win7 PHP Version: 7.0.7 Block user comment: N Private report: N New Comment: Ah, okay; So not a bug, then? In that case, this report can be closed. Thank you for the reply, nikic. :-) PS, considering the above, perhaps there could be a minor revision of the documentation (for people such as myself that might be confused by this in the future)? Currently, in the documentation for include, it has: "When a file is included, the code it contains inherits the variable scope of the line on which the include occurs." Recommended revision (or something along the lines thereof): "When a file is included, the code it contains inherits the variable scope of the line on which the include occurs (but maintains a separate execution context)." Previous Comments: ------------------------------------------------------------------------ [2016-06-18 13:54:16] nikic@php.net "include" in PHP is not "#include" in C. It doesn't just copy&paste your code. The included file has a separate execution context, even though it does inherit some things, most notably the symbol table from the including scope. You will also notice that writing "return" in the included file acts as a return from the file, not from the function it might be included in. This kind of cross-file goto is definitely not intended to work. ------------------------------------------------------------------------ [2016-06-18 13:52:35] maikuolan at gmail dot com Notes: - Bug seems to also apply to "foreach" (so, both "foreach" and "while" are affected by this). However, the bug doesn't seem to apply to "for" (we see the expected results when substituting for "for"). - "include", "include_once", "require" and "require_once" all have the same results; Doesn't seem to matter which of these are used. - Bug only applies when the break exists in an included/required file; If the loop and the break exist within the same file, the bug won't apply. ------------------------------------------------------------------------ [2016-06-18 13:45:36] maikuolan at gmail dot com Description: ------------ "while" not considered a part of the loop context when breaking from an included file. Test script: --------------- <?php /** a.php */ $a = false; while (true) { // Infinite loop if ($a) { break; } include_once 'b.php'; } echo 'This message will never be echoed due to this possible bug.'; <?php /** b.php */ $a = true; continue; Expected result: ---------------- Expected: "This message will never be echoed due to this possible bug.". Actual result: -------------- Actual: "PHP Fatal error: 'continue' not in the 'loop' or 'switch' context in \b.php on line 5". ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72445&edit=1

« previous php.bugs (#201716) next »