循环内的 ob_start()
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧 - 你不会相信 - 一旦我发布了这个问题 - 在我意识到问题出在哪里之后 - 我正在使用 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!
每次你要在循环中多次使用同一个文件时,你不应该使用 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!
为什么要循环?
extract($this->_records);
要短一些
看起来比原生的
,而且 var_dump 有时会很有帮助(下次你遇到这样的麻烦时):)
Why looping?
extract($this->_records);
looks a bit shorter than
and native in addition
and var_dump is a great help sometimes (for the next time you run into trouble like this one) :)