【问题标题】:Is there a way to exit only the php file being included?有没有办法只退出包含的 php 文件?
【发布时间】:2011-07-17 20:55:48
【问题描述】:

所以,我有一个包含在index.php 中的sidebar.php。在某种情况下,我想让sidebar.php 停止运行,所以我想把exit 放在sidebar.php 中,但这实际上会退出它下面的所有代码,这意味着index.phpinclude('sidebar.php'); 下的所有代码都会也被跳过。有没有办法让exit 只跳过sidebar.php 中的代码?

【问题讨论】:

    标签: php


    【解决方案1】:

    只需使用return;

    请注意,以这种方式实际上可以将某些内容返回给调用脚本。

    如果你的父脚本有 $somevar = include("myscript.php"); 然后在 myscript.php 你说... return true; 你会在$somevar 中得到那个值

    【讨论】:

      【解决方案2】:

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

      <?php
      
      if($certain_condition) {
          return;
      } else {
          // Do your stuff here
      }
      
      ?>
      

      【讨论】:

        【解决方案3】:

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

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

        我希望这对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 2010-11-05
          • 1970-01-01
          • 2022-01-06
          • 2020-06-23
          • 2022-01-05
          • 2020-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多