php obstart() ,$output2= ob_get_clean();

php obstart() ,$output2= ob_get_clean();

我在我以前的所有程序中都使用了 ob_start() 和 $output2= ob_get_clean() 但是突然间,对于这个当前程序,我遇到了“未定义的变量 $output2”,当我运行 程序。如果有人看一下,我将不胜感激

代码:

<ul>
<?php
require_once "classes/DBAccess.php";
// remoteserver I used db.php Elh wants to get database setting from *** for the first time, early bird
include "settings/db.php";
$db = new DBAccess($dsn, $username, $password);
$pdo = $db->connect();
$sql = "select categoryName, categoryId from category";
$stmt = $pdo->prepare($sql);
$rows = $db->executeSQL($stmt);
$rows = $pdo->query($sql);
foreach ($rows as $row):
$id = $row["categoryId"];
$name = $row["categoryName"];
?>
<li><a href="hype.php?id=<?= $id ?>"><?= $name ?></a></li>
<?php endforeach;
ob_start();
$output2= ob_get_clean();
include "templates/layout.html.php";
$pdo = null;
?>
</ul>

输出缓冲通过拦截所有输出来工作; ob_start() 和 ob_end_clear() 之间的任何内容都会被拦截; ob_start() 之前的任何输出都不是。

在这种情况下,由于这 2 个语句紧随其后,因此您根本没有拦截任何内容。