2010-12-09 117 views
1

有什麼方法可以通過include('to_include.php')檢查包含的文件是否已返回任何內容?如何檢查include()是否返回任何內容?

這是它的外觀:

//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(),但在我當前的設置中這是不可能的。

回答

1

使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; 
} 
+0

你聰明*§!?&#:-) – maartenmachiels 2010-12-09 14:31:22

1

如果你在談論生成的輸出,你可以使用:

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_電話......我認爲這是正確的,從內存,但內存是一個金魚。

您也可以在include文件中設置變量的內容,如$GLOBALS['done'] = true;,它會生成一些內容並在您的主代碼中檢查該內容。

+0

謝謝!我會試試這個。 – maartenmachiels 2010-12-09 14:17:59

0

我不知道如果我錯過了問題的要點,但是....如果函數定義

function_exists(); 

將返回true。

include() 

如果文件被包含,則返回true。

所以包裹一方或雙方在if(),你是好去,除非我得到了棍子

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

感謝您的見解,但是否包含該文件不是問題。我的問題是關於是否包含的文件生成任何東西。你有什麼想法嗎? – maartenmachiels 2010-12-09 14:13:55

+0

function_exists()將測試包含文件中的函數是否存在。如果你想看看它是否會輸出肯定你只是得到函數返回一個值... – piddl0r 2010-12-09 14:17:24

1

鑑於問題的措辭,它的聲音,如果你想這樣的:

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

哪個應該適合您的情況。這樣你就不必擔心輸出緩衝,變量蠕變,

0

嘗試

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