PHP包含没有输出? [英] PHP include without output?

查看:90
本文介绍了PHP包含没有输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,

继承人的事情。我有一个(付费的)购物车,目前我正在用jquery和css进行爵士乐和其他任何使它看起来和工作比现在更好的过程。无论如何,

为了使用他们的数据库及其功能等,我需要将他们的文件包含在网页的开头。我把它放在

 < div style =display:none;> 

当然,任何输出仍然会写入浏览器。它不是很多,但我会优先考虑没有div和NO输出,但我仍然希望它执行它的计算。它的5000行PHP文件,它太复杂了,通过编辑我不想要的位。



有一个聪明的方法,我可以包含文件,但指定如果我想在包含它的时候写入页面的任何内容?

我知道这有点闪避,我希望我已经解释了我自己,如果任何人都可以帮助,请让我知道。



谢谢。 您可以使用输出缓冲将输出捕获到变量中,而不是发送给浏览器:

  ob_start(); 
需要(...);
$ data = ob_get_clean();

请注意使用 需要 而不是 include 。这是因为我个人认为脚本应该在遇到包含错误时崩溃,而不是像没有错误那样继续。


Alright,

Heres the thing. I have a (paid for) shopping cart, which I am currently in the process of jazzing up with jquery and css and whatever else to make it look and work much nicer than it currently is.

Anyway. In order to use their database and their functions etc, I need to include their file at the beginning of the webpage. I have put it in a

<div style="display:none;">

But of course any output is still written to the browser. its not alot, but I would prefere to have NO div and NO output, but i still want it to perform it's calculations. Its 5000 line PHP file and Its too complicated to go through and edit out the bits I dont want.

is there a clever way, that I can include the file but specify if I want anything written to page when including it?

I knows it's a bit of a bodge, I hope i've explained myself, if anyone can help please let me know.

Thanks.

解决方案

You can use output buffering to catch output into a variable instead of sending it to the browser:

ob_start();
require(...);
$data = ob_get_clean();

Note the use of require instead of include. That is because I personally think that the script should crash when hitting an include error, not continue as if nothing is wrong.

这篇关于PHP包含没有输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆