PHP: @include_once 用于动态加载的内容?

PHP: @include_once for dynamically loaded content?

我有这个 header.php,我将它包含在我需要的所有页面中。我使用 @include_once('header.php'); 是为了能够通过 AJAX 将页面加载到另一个页面的 "container" div 而不必担心它被显示两次。但是由于主页和加载页面都包含 header,因此 header 显示了两次。我认为 include_once 不应该让这种情况发生。我该怎么办?

Jon 的建议是 "right way" -- re-architect 您的系统,因此仅在需要时包含 header。

但是,作为一种(计算成本高昂的)解决方法,在通过 AJAX 包含 header 的代码中,您可以检查是否存在仅存在于 [=17= 中的元素]:

//We know that the string "<div id='header'> exists only in header.php
if( $('#header').length < 1 )
{
     //your including header.php code here
}