Ob_start () in a loop

I had a problem while looping using the foreach () loop and inside that loop using ob_start () and ob_get_clean ().

Here is my function:

protected function renderEmail() {
$template = $this->_case.".php";
if(is_file($this->_dir.DS.$template)) {
    ob_start();
    if(!empty($this->_records)) {               
        foreach($this->_records as $key => $value) {
            ${$key} = $value;
        }
    }
    require_once($this->_dir.DS.$template);
    return ob_get_clean();
} else {
    $this->_errors[] = "Email template not found";
    return false;
} }

This function basically generates email content and then returns it.

The problem is when I look at multiple email addresses - to send the same email content - only the first one returns the contents - the next ones empty - any idea why?

+3
source share
3 answers

- - - , , , require_once(), - , () !

+11

?

extract($this->_records);

foreach($this->_records as $key => $value) {
    ${$key} = $value;
}

native

var_dump ( , :)

0

Each time you intend to use the same file several times inside a loop, you should never use user_once () or include_once, use "include" instead, and everything will be fine!

0
source

Source: https://habr.com/ru/post/1772846/


All Articles