Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include without output? [duplicate]

Tags:

html

include

php

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.

like image 206
Chud37 Avatar asked Nov 29 '25 04:11

Chud37


1 Answers

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.

like image 99
Emil Vikström Avatar answered Dec 01 '25 17:12

Emil Vikström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!