简体   繁体   中英

Using ob_ stuff inside a function and call it more than once

I just discovered, with my great surprise, that I can't use ob_ functions inside a user defined function and call this function more than once, because the second output never comes out

Here are my simplified files

index.php

function foo($data){
    ob_start();
    require_once("tpl.php");
    $html = ob_get_clean();
    return $html;   
}

echo foo('Hello');
echo foo('World!');

tpl.php

<p>and now I say... <?php echo $data; ?><p>

I'd expect this output:

and now I say... Hello
and now I say... World!

Instead, I get only this:

and now I say... Hello

Where am I wrong? Is there something I'm missing? I'm with PHP 5.3... Thank you in advance

Drop the _once part. Replace:

require_once("tpl.php");

with:

replace("tpl.php");

or:

include("tpl.php");

Else, you only use the template file once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM