InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress How-Tos

  • angularjs in wordpress
  • Convert wordpress page to posts and vice versa
  • Find all user created wordpress custom field keys
  • How to automate wordpress sandbox setup on Linux
  • How to capture php code or included file output in a variable
  • How to check the performance of a plugin using mysql query log
  • How to migrate wordpress from root to sub directory
  • How to upgrade wordpress manually
  • How to view wordpress current version
  • Setup xdebug for remote wordpress debugging
  • Wordpress - disable theme and plugin editing
  • Wordpress - get wpdb class method names
  • Wordpress multisite network vs normal installation
  • display method names from php WP_Query object
  • find if a wordpress page is leaf page
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress How To

How to capture php code or included file output in a variable

By admin | Last updated on Mar 20, 2016

Sometimes we want to reuse a php function or include a file and want to capture  the output (from echo, print, etc.) of that function/file to a variable. I had to do this in wordpress but the code is same for any php software.

Capture php output of file include

We can use php functions ob_start() and ob_get_clean() to start output buffering and get the content of the buffer. Here is sample php code snippet:

ob_start();
include(ABSPATH . 'path/to/include/file.php');
$content = ob_get_clean();

ob_get_clean() gets the buffer content, cleans the buffer and ends the buffering. There are couple of other ob_ functions for more flexibility. But I was able to manage with just these two.

Capture php output of function call

Here is sample php code snippet for function call

ob_start();
function_foo("var1", "var2");
$content = ob_get_clean();

Nested ob_start calls

ob_start and ob_get_clean() calls can be nested also. Here is how cascaded calls will look like:

<?php
function f1() {
  echo "f1";
}
function f2() {
  echo "f2";
}

ob_start();
f1();
ob_start();
f2();
$content_inner = ob_get_clean();
$content_outer = ob_get_clean();
print "content_inner=$content_inner\n";
print "content_outer=$content_outer\n";
?>

Here is the the outcome of above code when executed using php on command line:

content_inner=f2
content_outer=f1

Benefits

Here are some benefits of output buffering:

  1. It becomes much easier to re-use an existing code which you don’t want to modify.
  2. Often keeping html code in separate file with php embedded in it is more convenient than it being is pure php form. Using output buffering that code output can easily be capture in a variable.

Suggested posts:

  1. How to open vim at previous location
  2. How to use google custom search for wordpress site
  3. Command line – top IP list from apache access log
  4. display method names from php WP_Query object
  5. WordPress – how to make a post sticky on home page
  6. View http headers in Chrome
  7. How to set different HTTP Expire Header for multiple images of same type
  8. Python/Perl/Unix one liners
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PHP, Tutorials, Wordpress, Wordpress How To
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress