Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:31:46+00:00 2026-05-27T22:31:46+00:00

I have a simple PHP function that outputs HTML. <?php function get_header() { ?>

  • 0

I have a simple PHP function that outputs HTML.

<?php
function get_header() {
?>
<div id="header">
  <div class="page-width">
  <!-- And a lot more HTML after this line. -->
<?php
}
?>

So, when I call get_header(), the function outputs the HTML.

What is the simplest option to tweak this function to return the HTML as a string? Do I need to create a wrapper around this function? In other words, I’d like to be able to do e.g. var html_string = get_header_wrapper(), where html_string contains all the HTML above.

One thing I could think of is to duplicate the function and make it return a string. However, that would be so inefficient because it introduces a lot of code duplicate.

<?php
function get_header_wrapper() {
  var ret = <<<EOD
  <div id="header">
    <div class="page-width">
    <!-- And a lot more HTML after this line. -->
  ...
  EOD;

  return ret;
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:31:47+00:00Added an answer on May 27, 2026 at 10:31 pm

    You can make use of output bufferingDocs to get the output of that function:

    ob_start();
    get_header();
    $html = ob_get_clean();
    

    If you need that more than once, you can wrap it into a function of it’s own:

    /**
     * call a function and return it's output as string.
     * 
     * @param callback $function
     * @param array $arguments (optional)
     * @param var $return (optional) the return value of the callback
     * @return string function output
     */
    function ob_get_call($function, array $arguments = array(), &$return = NULL)
    {
        ob_start();
        $return = call_user_func_array($function, $arguments);
        $buffer = ob_get_clean();
        return $buffer;
    }
    

    Usage:

    $html = ob_get_call('get_header');
    

    As the answer is that popular today, here is another function to get the output of an include:

    /**
     * include a file and return it's output as string.
     * 
     * @param string $file
     * @param array $variables (optional) keys as variable names and values as variable values
     * @param var $includeReturn (optional) the return value of the include
     * @return string function output
     */
    function ob_get_include($file, array $variables = array(), &$includeReturn = NULL)
    {
        $includeFilename = $file;
        unset($file);
        extract($variables);
        unset($variables);
        ob_start();
        $includeReturn = include($includeFilename);
        return ob_get_clean();
    }
    

    Usage:

    include.php:

    <div class="greeting">
        Hello <em><?php echo htmlspecialchars($name); ?></em>!
    </div>
    

    Using:

    $variables = array(
        'name' => 'Marianne',
    );
    $html = ob_get_include('include.php', $vars);
    

    Related:

    • Answer to *Load result of php code instead of the code as a string
    • Answer to Is include()/require() with “side effects” a bad practice?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP file with one simple echo function: echo 'アクセスは撥ねりません。'; but when
I have a simple php script on a server that's using fsockopen to connect
I have this simple php script <?php echo '<pre>'; // Outputs all the result
I have a simple php script on my domain that sends me an email:
I have a simple PHP app that prints 'hello world'. When I run it
I have a simple PHP script that will either serve up a streaming ASF
I have a class with several functions that can render images. // render.php class
I have the following form in cakephp: <div class=quickcontactDisplay> <?php echo $form->create('Enquiry',array('action'=>'add','class'=>'quickcontact')); echo $form->hidden('visitor_id',
I have a PHP page which generates an HTML table. At the bottom of
I have two files. location.php, that outputs this: [[javascript],[PHP]] and in another file: <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.