Is it possible to stop including a php file?

前端 未结 3 733
醉梦人生
醉梦人生 2020-12-06 10:22

Is it possible to stop the inclusion of a file in the middle of a file?

For example, one file would contain:

include(\'home.php\');

相关标签:
3条回答
  • 2020-12-06 10:53

    In place of break, use return:

    return

    If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file.

    0 讨论(0)
  • 2020-12-06 10:57

    What you can do is set a flag in the "parent" script that the included script looks for. If it's set, it'll halt execution early, otherwise it'll continue as normal.

    Example:

    main.php

    <?php
    $_GLOBAL['is_included'] = true;
    include('somefile.php');
    // More stuff here
    ?>
    

    somefile.php

    <?php
    // Does some stuff
    
    // Stops here if being include()'d by another script
    if ( isset($_GLOBAL['is_included']) ) { return; }
    
    // Do some more stuff if not include()'d
    ?>
    
    0 讨论(0)
  • 2020-12-06 11:02

    No. When you include or require. This file gets loaded and parsed entirely. It does not mean that the code in it is executed, but it is loaded entirely.

    If you want to have a different flow of execution of this code, then you would need to use some of the control structures.

    http://php.net/manual/en/language.control-structures.php

    0 讨论(0)
提交回复
热议问题