在 PHP 中有一种情况,我需要主要执行页面,但是在该页面的输出中插入了一个项目。
我认为输出缓冲可能会有所帮助,但我不知道如何在我的情况下实现它。
我的代码如下所示:
//this document is part of a global functions file
function pageHeader (){
//I'm using $GLOBALS here because it works, however I would really rather a better method if possible
$GLOBALS['error_handler'] = new ErrorHandler(); //ErrorHandler class sets a function for set_error_handler, which gets an array of errors from the executed page
require_once($_SERVER['DOCUMENT_ROOT'].'/sales/global/_header.php');
//I would like the unordered list from ->displayErrorNotice() to be displayed here, but if I do that the list is empty because the list was output before the rest of the document was executed
}
function pageFooter (){
$GLOBALS['error_handler'] ->displayErrorNotice(); //this function displays the errors as an html unordered list
include($_SERVER['DOCUMENT_ROOT']."/sales/global/_footer.php");
}
网站上的大多数页面都包含此文档并使用pageHeader()
和pageFooter()
功能。我想要实现的是在包含 _header.php 之后将 PHP 生成的错误的无序列表放入 HTML 列表中。如果我把它放在页脚(在文档执行之后),我可以让列表按预期工作,但我不希望它在那里。我想我可以用 JS 移动它,但我认为必须有一个 PHP 解决方案。
更新
我想知道ob_start()
通过正则表达式搜索缓冲区的回调函数将错误列表放在哪里,然后将其插入将成为解决方案。
更新 2我已经解决了这个问题,我的答案如下。当我被允许时,我会在 2 天内接受它。