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');
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.
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
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
somefile.php
In place of
break
, usereturn
: