php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72445 while not considered a part of loop context when breaking from included file.
Submitted: 2016-06-18 13:45 UTC Modified: 2016-06-18 13:54 UTC
From: maikuolan at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 7.0.7 OS: Win7
Private report: No CVE-ID: None
 [2016-06-18 13:45 UTC] 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".

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-18 13:52 UTC] 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:54 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2016-06-18 13:54 UTC] 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 14:05 UTC] maikuolan at gmail dot com
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)."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 10:01:28 2024 UTC