简体   繁体   中英

How to check if an include() returned anything?

Is there any way to check if an included document via include('to_include.php') has returned anything?

This is how it looks:

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

So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.

I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php , but that's not possible in my current setup.

If you are talking about generated output you can use:

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

Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.

You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.

make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:

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

Given the wording of the question, it sounds as if you want this:

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

Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc .

I'm not sure if I'm missing the point of the question but ....

function_exists();

Will return true if the function is defined.

include() 

returns true if the file is inclued.

so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick

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

try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM