InfoHeap
Tech tutorials, tips, tools and more
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. PHP – how to use function static variable as cache
  2. How to include angularjs javascript in wordpress post
  3. Running php eval on code with tags
  4. Bash – set default value if a variable is empty
  5. Javascript check if variable is defined
  6. How to find recursion stack growth direction using C code
  7. WordPress – how to display code from a file in textarea
  8. PHP execute command and capture shell exit status
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PHP, Tutorials, Wordpress, Wordpress How To

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | 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

Copyright © 2021 InfoHeap.

Powered by WordPress