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

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Programming

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.

Related posts:

  1. form checkboxes in php array and storing variables in url string
  2. php switch/case statements

Leave a Reply