如何检查 include() 是否返回任何内容?
有什么方法可以检查通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
让
function_that_generates_some_html_sometimes_but_not_all_the_times()
在输出某些内容并设置变量时返回某些内容:make
function_that_generates_some_html_sometimes_but_not_all_the_times()
return something when it outputs something and set a variable:如果您正在谈论生成的输出,您可以使用:
可能需要调整 ob_ 调用...我认为这是正确的记忆,但记忆是金鱼的记忆。
您还可以在生成某些内容时在包含文件中设置变量的内容,例如
$GLOBALS['done'] = true;
,并在主代码中进行检查。If you are talking about generated output you can use:
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.鉴于问题的措辞,听起来好像您想要这样:
哪个应该适合您的情况。这样您就不必担心输出缓冲、变量蠕变等等。
Given the wording of the question, it sounds as if you want this:
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
我不确定我是否错过了问题的要点,但是......
如果定义了函数,将返回 true 。
如果包含该文件则返回 true。
因此,将其中一个或两个都包含在 if() 中,就可以开始了,除非我搞错了方向
I'm not sure if I'm missing the point of the question but ....
Will return true if the function is defined.
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
尝试
try