如何检查 include() 是否返回任何内容?

发布于 2024-10-07 04:04:41 字数 590 浏览 0 评论 0原文

有什么方法可以检查通过 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() ,但这在我当前的设置中是不可能的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

饭团 2024-10-14 04:04:41

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

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; 
}
<逆流佳人身旁 2024-10-14 04:04:41

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

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; ,并在主代码中进行检查。

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.

淤浪 2024-10-14 04:04:41

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

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

哪个应该适合您的情况。这样您就不必担心输出缓冲、变量蠕变等

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.

樱娆 2024-10-14 04:04:41

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

function_exists();

如果定义了函数,将返回 true 。

include() 

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

因此,将其中一个或两个都包含在 if() 中,就可以开始了,除非我搞错了方向

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

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
}
丘比特射中我 2024-10-14 04:04:41

尝试

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

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; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文