PHP: Write Output of a Script into Variable
Tutorial by Stefan Trost | Last update on 2021-05-14 | Created on 2015-02-02
In this tutorial, I would like to show you how to write the output of an arbitrary PHP script into a variable instead of sending it to the browser.
This can be used, for example, when we would like to send a script output via e-mail or if we would like to process the output in any other way (writing output results into database, mailing output notifications of cronjobs and so on).
For this purpose, we are using Output Buffering which can be started by calling the function ob_start(). After ob_start() is executed, the output of the script will no longer be sent. Instead, the output is stored into an internal buffer that we can read out later.
A simple Example
Let us look at a small example for this procedure.
// start output buffering ob_start(); // create an output echo "ABC"; // read out buffered content $output = ob_get_contents(); // finish output buffering ob_end_clean(); // echo content of variable echo $output; // ABC
First of all, we are starting the output buffering with ob_start() and let the script output something (here "ABC"). Using the function ob_get_contents() we can read out the internal buffer, here we are saving the buffered content into the variable $output. Subsequently, we are finishing the output buffering with ob_end_clean(). From this time on, the script can create normal output again and we are outputting the variable $output in which we have stored the output made before.
Of course, instead of outputting the content of the variable, we can also process it or send it via e-mail, which is making more sense, but here, this small example should be sufficient.
Additional Functions
By the way, we can determine the length of the current internal buffer with the function ob_get_length() and we can delete the current internal buffer before the output buffering was finished by using ob_clean(), for example when it is necessary to work with several variables. When using ob_end_flush() instead of ob_end_clean(), the output buffering is also stopped, but with this function the output is sent nevertheless - so, using this, it is possible to do both, storing the output into a variable as well as showing the output in the browser window.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
PHP: Send Output of a Script by Mail
Tutorial | 0 Comments
jQuery: Send HTML5 Canvas to Server via Ajax
Tutorial | 0 Comments
PHP: File Download Script
Tutorial | 0 Comments
PHP: Sending an E-Mail
Tutorial | 0 Comments
Send HTML5 Canvas as Image to Server
Tutorial | 0 Comments
jQuery: Submit complete form and receive content with Ajax
Tutorial | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.