【问题标题】:How to check if an include() returned anything?如何检查 include() 是否返回任何内容?
【发布时间】:2011-05-22 21:22:08
【问题描述】:

有什么方法可以检查通过include('to_include.php') 包含的文档是否返回了任何内容?

看起来是这样的:

//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();

//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') { 
    echo $do_a_little_dance_make_a_little_love_get_down_tonight; 
}

因此,在我的主文档中包含to_include.php 之后,我想检查包含的文档是否生成了任何内容。

我知道显而易见的解决方案是在main_document.php 中使用function_that_generates_some_html_sometimes_but_not_all_the_times(),但这在我当前的设置中是不可能的。

【问题讨论】:

    标签: php include return


    【解决方案1】:

    make function_that_generates_some_html_sometimes_but_not_all_the_times() 在输出一些东西并设置一个变量时返回一些东西:

    //to_include.php
    $ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
    
    //main_document.php
    $ok='';
    include('to_include.php');
    if($ok != '') { 
        echo $do_a_little_dance_make_a_little_love_get_down_tonight; 
    }
    

    【讨论】:

      【解决方案2】:

      如果您在谈论生成的输出,您可以使用:

      ob_start();
      include "MY_FILEEEZZZ.php";
      function_that_generates_html_in_include();
      $string = ob_get_contents();
      ob_clean();
      if(!empty($string)) { // Or any other check
          echo $some_crap_that_makes_my_life_difficult;
      }
      

      可能需要调整 ob_ 调用...我认为这是记忆中的,但记忆是金鱼的记忆。

      您也可以在生成某些内容时在包含文件中设置 $GLOBALS['done'] = true; 等变量的内容,并在您的主代码中检查它。

      【讨论】:

        【解决方案3】:

        鉴于问题的措辞,听起来好像你想要这样:

        //to_include.php
        return function_that_generates_some_html_sometimes_but_not_all_the_times();
        
        //main_document.php
        $the_return_of_the_include = include 'to_include.php';
        if (empty($the_return_of_the_include)) { 
            echo $do_a_little_dance_make_a_little_love_get_down_tonight; 
        } else {
            echo $the_return_of_the_include;
        }
        

        这应该适用于您的情况。这样您就不必担心输出缓冲、变量蠕变、

        【讨论】:

          【解决方案4】:

          我不确定我是否错过了问题的重点,但是....

          function_exists();
          

          如果函数已定义,则返回 true。

          include() 
          

          如果包含文件,则返回 true。

          所以将其中一个或两个都包含在 if() 中,你就可以开始了,除非我弄错了

          if(include('file.php') && function_exists(my_function))
          {
           // wee
          }
          

          【讨论】:

          • 感谢您的见解,但是否包含该文件不是问题。我的问题是关于包含的文件是否生成了任何东西。你对此有什么想法吗?
          • function_exists() 将测试包含文件中的函数是否存在。如果你想看看它是否肯定会输出,你只需让函数返回一个值......
          【解决方案5】:

          试试

          // to_include.php
          $returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
          echo $returnvalue;
          
          //main_document.php
          include('to_include.php');
          if ( $returnvalue != '' ){
             echo $do_a_little_dance_make_a_little_love_get_down_tonight; 
          }
          

          【讨论】:

          • 谢谢,就是这样!但是卡特林是第一个;-)
          猜你喜欢
          • 2010-09-17
          • 2011-03-13
          • 2011-11-26
          • 2021-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多