PHP include without output? [duplicate]

2019-08-13 10:38发布

This question already has an answer here:

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.

标签: php html include
2条回答
劫难
2楼-- · 2019-08-13 11:12

Check out ob_start() and ob_end_clean(). They allow you to capture the output and dump it which should fix your problem. You might also want to talk to your vendor about creating a non-outputing version of their software which is really what it should be doing (I know preaching to the chorus).

I never actually knew of a valid reason to use ob-end-clean until now. :-)

查看更多
甜甜的少女心
3楼-- · 2019-08-13 11:20

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.

查看更多
登录 后发表回答