Is it possible to stop including a php file?

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

For example, one file would contain:


and in home.php, it would try to cancel the inclusion at some point:

break; // I tried it doesn't work
echo "this will not be output

I’m not talking about an exit, which stops everything, even the root file. I just want the current file to be exited from.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

In place of break, use 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.

Solution 2

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.

Solution 3

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.



$_GLOBAL['is_included'] = true;
// More stuff here


// 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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply