循环内的 ob_start()

发布于 2024-09-30 14:21:25 字数 677 浏览 0 评论 0原文

使用 foreach() 循环进行循环以及在该循环内部使用 ob_start() 和 ob_get_clean() 进行循环时遇到问题。

这是我的函数:

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;
} }

该函数基本上生成电子邮件的内容,然后返回它。

我遇到的问题是,当我循环访问多个电子邮件地址时 - 发送相同的电子邮件内容 - 只有第一个地址返回内容 - 接下来的地址是空白的 - 知道为什么吗?

I've got a problem when looping using foreach() loop and inside of this loop using ob_start() and ob_get_clean().

Here's 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 is basically generating content of the email and then returns it.

The problem I have is when I loop through a number of email addresses - to send the same email content - only the first one returns the content - the following ones are blank - any idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

段念尘 2024-10-07 14:21:25

好吧 - 你不会相信 - 一旦我发布了这个问题 - 在我意识到问题出在哪里之后 - 我正在使用 require_once() 函数 - 它可以防止再次包含相同的文件 - 一旦更改为包含() 一切正常!

Ok - you won't believe - once I've posted this question - straight after I've realised where the problem was - I'm using require_once() function - which prevents the same file to be included again - once changed to include() everything works fine!

許願樹丅啲祈禱 2024-10-07 14:21:25

每次你要在循环中多次使用同一个文件时,你不应该使用 require_once() 或 include_once,而是使用“include”,一切都会好起来的!

Every time you are going to use a same file several times inside a loop, you should never user require_once() or include_once, instead use, 'include', and everything will be fine!

此刻的回忆 2024-10-07 14:21:25

为什么要循环?

extract($this->_records);

要短一些

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

看起来比原生的

,而且 var_dump 有时会很有帮助(下次你遇到这样的麻烦时):)

Why looping?

extract($this->_records);

looks a bit shorter than

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

and native in addition

and var_dump is a great help sometimes (for the next time you run into trouble like this one) :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文