Understanding ob_start() function in php

513 views Asked by At

I have a php as shown below in which I am trying to understand ob_start() function in php.

// Prepare for application Parsing {{{1
function ob_application_framer($content)
{
    global $report;
    if (!isset($_GET['hello'])) {
        $content = ob_application_handler($content);
    }

    if (isset($_GET['world'])) {
        $_SESSION['top'] = str_replace("<head>", "<head><base target='view_panel' />", $content);
        $_SESSION['bottom'] = '<!--Empty-->';

        return "<html>
    <head>
        <title>A*pplication Report Frame View: " . $report->getFirst('Title') . "</title>
    </head>
    <frameset rows='*,250'>
        <frame src='../top.php' />
        <frame src='../bottom.php' name='view_panel' />
    </frameset>
</html>";
    } else {
        return $content;
    }
}


if (!isset($_GET['hello']) || isset($_GET['world'])) {
    echo "I am the right place";        // Line A
    ob_start('ob_application_framer');  // Line Z
}

The value of $_GET is Array ( [report] => ./history.php )

On the webpage, it prints the following which is coming from Line A:

I am the right place

Problem Statement:

I am wondering why the code doesn't go inside ob_application_framer function at Line Z.

1

There are 1 answers

5
Rakesh Mehta On

As per the doc https://www.php.net/manual/en/function.ob-start.php the ob_start function sets output buffering ON, so that all echo() print_r() or HTML will not be sent to the output until you explicitly tell PHP to do that by calling ob_flush() or other similar functions or your script reaches to its end.

The second parameter of ob_start is a callback function. When your script ends or you explicitly call ob_flush() function, then your callback will be called and the parameter of your callback function will have the content that is gathered in buffer. Now you can play with that content and then finally return from your function. Whatever you return from your function, will be sent to the output.

Where is your "ob_application_handler()" defined?

Make sure that your ob_start callback function does not have any error. To verify it, invoke the function explicitly and make sure you don't have any error there, otherwise you won't get any output.

================== Edit =================

See here ...

https://paiza.io/projects/kfVp4XU37o1ewMYk_k8RzA

See the last line, I have just echoed "I am the right place", but the final output is "I am the right place something extra added from ob_application_framer()"..

Note: that I have added this extra code at the top of the example, as I had not idea of the global variables you are using within the functions. So I just created those classes and 2 global variables (that are used in your code) to make the code run.

class Report{
    public function getFirst($key){
        return "something";
    }
}

class ApplicationConfig{
    public function getAll($key){
        return [];
    }
}

global $report;
$report = new Report();


global $application_config;
$application_config = new ApplicationConfig();

The extra words are padded from within ob_application_framer(). So definitely your code is working and the callback of ob_start() is getting executed as it should be. Is anything particular you are trying to do?