Home » Php » Is there a way to exit only the php file being included?

Is there a way to exit only the php file being included?

Posted by: admin November 30, 2017 Leave a comment


So, I have a sidebar.php that is included in the index.php. Under a certain condition, I want sidebar.php to stop running, so I thought of putting exit in sidebar.php, but that actually exits all the code beneath it meaning everything beneath include('sidebar.php'); in index.php all the code would be skipped as well. Is there a way to have exit only skip the code in the sidebar.php?


Just use return;

Do also be aware that it is possible to actually return something to a calling script in this way.

if your parent script has $somevar = include("myscript.php"); and then in myscript.php you do say… return true; you will get that value in $somevar


Yes, you just use return;. Your sidebar.php file might look something like this:


if($certain_condition) {
} else {
    // Do your stuff here



I know this is a really old question, but I’ve recently taken over the code base of another developer who used exit religiously, meaning that the parent file that included various files had to be designed in such a way that the include of the module files were done at the end so it didn’t cut off the page. I wrote a small PHP script to replace all occurrences of “exit;” with “return;”.

if($handle = opendir("path/to/directory/of/files")) {
    while(false !== ($file = readdir($handle))) {
        if("." === $file) continue;
        if(".." === $file) continue;

        $pageContents = file_get_contents($file);
        $pageContents = str_replace("exit;", "return;", $pageContents);
        file_put_contents($file, $pageContents);

        echo $file . " updated<br />";


I hope this helps someone.