有没有办法只退出包含的PHP文件?

Str*_*rry 35 php

所以,我有一个sidebar.php包含在index.php.在一定条件下,我想sidebar.php停止运行,所以我想外放exitsidebar.php,但实际上退出所有的代码在其下方意义下的一切include('sidebar.php');index.php所有的代码将被跳过为好.有没有办法exit只跳过代码sidebar.php

Oli*_*pro 77

只是用 return;

还要注意,实际上可以通过这种方式将某些内容返回给调用脚本.

如果你的父脚本有$somevar = include("myscript.php");,然后在myscript.php中你说... return true;你将获得该值$somevar


Emm*_*uel 11

是的,你只是用return;.您的sidebar.php文件可能如下所示:

<?php

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

?>
Run Code Online (Sandbox Code Playgroud)


小智 6

我知道这是一个非常古老的问题,但我最近接管了另一个使用exit出口的开发人员的代码库,这意味着包含各种文件的父文件必须以包含模块的方式设计文件最后完成,所以它没有切断页面.我写了一个小的PHP脚本来替换所有出现的"退出"; 与"回归;".

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

    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助别人.