ob_start - save php output to a string - The PHP output buffering

I want to share this great php utility: the output buffering, it is sometimes very usefull. So what it does ?
The PHP output buffering will save all the server outputs ( html and php prints) to a string variable.

So to start buffering, use ob_start(); this will keep saved any output.
Then you use $variable = ob_get_clean(); to stop buffering, and copy the buffer content to the variable.

Here are few samples of the use of ob_start() and ob_get_clean()


<?php ob_start(); //Turn on output buffering ?>
Hello world, <a href="http://www.blogger.com/myotherpage.php">link</a>
<div class="style">Content</div>
<?php $var = ob_get_clean(); //copy current buffer contents into $message variable and delete current output buffer ?>


Now the content of the variable $var should be:

Hello word, <a href="http://www.blogger.com/myotherpage.php">link</a>
<div class="style">Content</div>


Another example (with php outputs buffered)

<?php ob_start(); ?>
Hello world, <?php echo "My Website"; ?>
Thank you.
<?php $var = ob_get_clean(); ?>


And this will be the content of $var:
Hello world, My Website
Thank you.

That's all !

71 comments:

Jose Tapia said...

Good article very simple and concisous

Robbie said...

Thanks! Extremely helpful tip. I was looking for a way to output the OUTPUT of a php file using geshi, but of course using file_get_contents outputs the SOURCE, not the output.

This is what I came up with:

ob_start();
include('generatebash.php');
$currentoutput = ob_get_clean();
geshi_highlight($currentoutput, bash);

Again; thanks! Works like a CHARM!

ILyes said...

Thank you for your nice comments :)

Namas said...

Good article with nice examples...

rize wordpress said...

We have highly skilled software professionals to create complicated, enterprise-class frameworks effectively in PHP.
PHP Developers

Buy Guild Wars 2 Gold said...


it feels like you had a much more fabulous saturday and sunday compared to my very own! lol haya, my partner and i spent the idea researching regarding finals. ughhh.
lovely blog site, by the way! Diablo 3 cd key



GW2 Gems

rscredits said...

It's remarkable to see your nice runescape gold introduction, and I'm going to bookmark rs gold this and come back to it. Keep writing! I really love your blog and benefit of your buy runescape gold easy decriptions.

Guild wars 2 gold said...

Yet again fantastic article. You appear to have a good understanding of these types of styles.When I coming into your website,We thought this kind of . Think about it and make writting your site could be more appealing. In your Success! diablo 3 gold

smartsuite said...

I believe Web time sheet software makes the complete employee time clock tracking task easier. Its easy to update, approve and maintain the time sheets in no time.Time Attendance System

guildwars said...

To purchase gw2 gold online for game improvement, try to order on the professional Guild Wars 2 stores like gwsafe. All cheap guild wars 2 gold on our site is manually obtained by high-level proficient GW2 gamers who are good at farming. So, our gold online is totally safe and legit for you to directly use it in game, and will not get your account in any risk! Picking up our guild wars 2 gold for sale, you never need to worry about your account security, what you just need to do is just focusing on using the gold in game to fulfill your higher-level challenge!

Kang Wahid said...

nice share bro,

Ada Bush said...

It is a good package greater ones compared to plastic-type material ones G√ľnstige android-Handys ohne Vertrag online bestellen together with merchants on a regular basis.

fifa 14 ps4 coins said...

Just as before wonderful post. A person seem to possess a great knowledge of these kinds of designs. After i entering your site, All of us believed this sort of. Consider it as well as help to make writting your website might be more desirable.


fifa ultimate team coins
lol elo boosting
fifa coins
buy fifa coins

Moliy Yvonne said...

Thanks a lot for this awesome post, friend. It truly resonates with me, maybe you also like rsgoldaz

Roshini RS said...

I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
Regards,
PHP Course Chennai|PHP Training

Amirtha rao said...

This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing..
Regards,
Web design course in Chennai|Web design training Chennai

Lotus D said...

PHP (Hypertext Pre-Processor) language is recognized as one of the fastest growing web programming languages on the web industry as well as on the software development today.

Raksha said...

Hello, Can anyone help me about the career path and chances in choosing PHP? I have planned to do PHP Training in Chennai. Someone suggest where to join for the best PHP Training in Chennai where I could also get placement services?

Moliy Yvonne said...

Thanks a lot for this awesome post, friend. It truly resonates with me, maybe you also like rsgoldaz.com

Rosen Rose said...

PHP is one of the most popular server side scripting language. One of the striking feature of PHP is open source. PHP is light weight and easy to use scripting language comes with bunch of free open source software like WAPM, LAMP, XMPP (server platforms), MySQL(database platform) and much more.
php

SLA INSTITUTE Chennai said...

Great post. Thanks for sharing this post. PHP Training Center in Chennai

shankar said...

Thanks for sharing as it is an excellent post would love to read your future post..


CMMI Consulting in Chennai

Sandeep Goel said...

Thanks for stating the Start & Clean object functions here. We get the most out of your blog. Keep posting new updates so as to get the new information always. For the best PHP training in Indore seekers visit Srisasoft India now.

career zone Mohali said...

your site are very usefull for me.
php training | php classes | php center | php training institute in mohali chandigarh

geethu said...

Very nice information thanks for sharing and keep posting.
Web Designing Course in Chennai | Web designing training in Chennai | FITA Training

Saira said...

Very nice post Thanks for sharing informative blog about PHP Training

WT Labs said...
This comment has been removed by the author.
Priya Gupta said...

Found your blog. Its really nice. I appreciate your article. Its important to find top colleges for getting admission in a well known college in India. So thanks for sharing all that important information. management colleges in punjab

Deeksha Dawar said...
This comment has been removed by the author.
Indo Pusat said...

This news is very interesting and nice to read . It is very helpful and useful for all readers . passion to write an interesting article the other good . thank you

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training

kaurn said...

Great Information, Thank You. Corpits offers PHP Training in Chennai.

dev prog said...

it’s ok to show some appreciation and say ‘great post’
.NET developer

Deeksha Dawar said...

I read lot of articles and really like this article. This information is definitely useful for everyone in daily life. Fantastic job.PHP Training

Sathiaja Sarah said...

dear,
sir/mam
Currently there are (literally) millions of websites who claim to be professional web designers. However, one must be careful in choosing the right person or people in which to trust their company image.
See more at:website design Massachusetts

Zinavo Bangalore said...

The article is very interesting, have a great article. Thanks for sharing. Web Design Company Bangalore | Website Designing Bangalore

Best Multimedia (Dilsukhnagar Arena) said...

This article is intresting...web design course

Sathiaja Sarah said...

dear,
all mens/womens
Web design tricks are used by Professional web designers; to create effective web page designs but most importantly to show off beautiful code. A web designer's job is to develop web pages that communicate effective design, while also demonstrating knowledge of html and CSS.
See more at:website design Massachusetts

Deeksha Dawar said...

I read lot of articles and really like this article. This information is definitely useful for everyone in daily life. Fantastic job.Web Designing Training in Noida

SEO Travelbest said...

Just linked this article on my facebook account. it’s a very interesting article for all.

Web Development Company in Chennai

Pooja Sharma said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.web designing in jalandhar

Zinavo Bangalore said...

Thanks for sharing this kind of information. People will surely like this kind of information. Web Development Company Bangalore | Web Design Bangalore

Sathiaja Sarah said...

A good web designing company can assist you to get the perfect website for your business concern. Read on and get detailed information about the pre-requirements of a good web design company.
see more details:Logo Design Massachusetts

Anonymous said...

The post is very informative. It is a pleasure reading it. I have also bookmarked you for checking out new posts. web designing jalandhar

Zinavo Bangalore said...

This is very interesting, keep more sharing. Web Design Company Bangalore | Website Designing Bangalore

Sathiaja Sarah said...

Becoming a freelancing web designer and working from your home office is a career many would like to pursue. Find out what it takes to become a successful freelancer in this dynamic and ever-changing field.
see more details:Logo Design Massachusetts

Tanisha Singh said...

Want to master Email marketing training in 2016? Join Email Marketing Certification Course. Join FREE Demo!

Cncweb World said...

Thanks for sharing your knowledge. Very useful for everyone. Keep sharing.
php classes in pune

Rahul Kumar said...


Thanks for sharing this information. If anyone is looking for Web Designing training institute in noida, then please contact us.

Web Designing Training

Rekha J said...

This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai

Lenova Services said...

Thanks for sharing this valuable information.
lenovo laptop service center in chennai
lenovo thinkpad service center chennai
lenovo ideapad service center chennai

1croreprojects said...

Thanks for sharing this valuable information.
ieee java projects in chennai
ieee dotnet projects in chennai
mba projects in chennai
be projects in chennai
ns2 projects in chennai
mca projects in chennai
bulk projects in chennai
bsc projects in chennai
msc projects in chennai

Harini R said...

excellent guide its much useful for my students career keep share more things about java for us. thanks for shared your post. keep it up.
php training in chennai

Stuart Spindlow said...

Thank you for your nice comments :)
Digital Marketing Agency Thailand
Web Development Bangkok

Iqbal Dlk said...

very nice and informative blog
big data projects chennai
mobile computing projects chennai
cloud computing projects chennai
secure computing projects chennai

paulrobert932 said...

For best and cheap manual Seo services, Web designings. Must check Web Design Bangalore

apto inn said...

You post explain everything in detail and it was very interesting to read. Thank you. nata coaching centres in chennai

uttam said...

Thanks for sharing information about PHP programming language, I read your entire post and realize that your post is really informative. Such type of post is really helpful for those people who want to learn PHP programming language. Apart from this I read another informative post for same kind of information. For your further information I am sharing that informative post about PHP programming language.

Anu said...

Nice it seems to be good post... It will get readers engagement on the article since readres engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
iOS Training in Chennai
Android Training in Chennai
php Training in Chennai

Words Doctorate said...

Thank you for sharing such a informative information with us. Keep on sharing the blog like this.

PhD Thesis Writing Services
Dissertation Writing Services
Research Paper Writing Services
Master Thesis Writing Services

Aptron gurgaon said...

I may be crazy but, the idea has been nagging me for some time that perhaps the biggest favor we could do for the African poor would be to kill off all that dangerous wild life.
Best PHP Training in Gurgaon

Lucky Srivastava said...

Hey Very nice Blog,Thanks For Sharing..

Best Php industrial training in Lucknow
best summer training in lucknow
Embedded system training in lucknow
Best Industrial training company in lucknow
best Php training center

Mrwebtechnologies said...

we are Delhi based Web Design Company. but we deals all over india and overseas as well. we have Dubai, Singapore, hong kong , London customer as well.

Vamshi Krishna said...

Nice to read your article! very informative.So, please keep posting useful Stuff here Thanks.......

Lucky Srivastava said...

Hey Nice blog,Thanks for sharing this blog.!!!

Best python training in lucknow

Best Php training in lucknow

best summer training company in lucknow

Tina Kenard said...

I feel satisfied with your blog, you have been delivering useful & unique information to our vision even you have explained the concept as deep clean without having any uncertainty, keep blogging.PHP Training in Chennai|web designing training in chennai

A. Atschekzai said...

Thanks for your great information, the contents are quiet interesting.I will be waiting for your next post. webdesign firma

Lucky Srivastava said...

Hey Very nice Blog,Thanks For Sharing..

Python training in Lucknow
Softpro Learning Center
best Php training center
Best python training in lucknow

Lucky Srivastava said...

Hey Gyss Check out this...

Softpro Learning Center (SLC)is the training wing of Softpro India Computer Technologies Pvt.
Limited. SLC established itself in the year 2008.
SLC offer an intensive and extensive range of training/internship programs for B.Tech, BCA, MCA & Diploma students.
Softpro Learning Center is a best institute in Lucknow extends in depth knowledge of technology like .Net, Java, PHP and Android and also an opportunity to practically apply their fundamentals. SLC’s objective is to provide skilled manpower to support the vast development programs.

sai pallavi said...


I see your blog daily. your blog is very useful for me & i like so much.Php Jobs In Hyderabad

Lucky Srivastava said...

Hey Nice blog,Thanks for sharing this blog.!!!


Best Summer training in lucknow

Best Php training in lucknow

Summer training in lucknow

Summer training Company in lucknow