Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog.

capturing and storing echo or include data into a php variable

Have you ever included a file into your php web application or used a function that executes an echo rather than a return? Have you ever wanted to capture the data returned into a variable instead? Well here’s how:

1
2
3
4
ob_start(); // always start with this
the_title(); // this function does an echo
$title = ob_get_contents(); // this grabs the echo and stores the data as $title
ob_end_clean(); // always end with this

This will store the data echoed from the_title() as $title. For more information take a look at PHP.net’s entry on ob_start.

Leave a Reply