将 PHP include 语句嵌入到得到回显的字符串中

发布于 2024-10-17 05:37:37 字数 1491 浏览 2 评论 0原文

我一直在尝试通过将我的网站结构定义为 php 字符串变量然后回显这些变量来清理我的网站。这将我的所有站点结构压缩为五行,并在 echo 语句之间插入一篇文章。我的页面如下所示:

包含('模块/moddefault.php'); //链接到设置为变量的站点结构
$title = "示例页面"; // 定义页面标题。这被插入到嵌入的标题语句中。

回显“$modtop”; // 声明、标题、标题等
回显“$precontent”; // 预内容结构
?>

此处显示网页内容


文章内容会很好并且整洁。

?>

我想知道如何将 CSS 嵌入到 PHP 字符串变量中。 PHP 包含不注册,因为 php 已经处理了该页面。 include 语句被打印到我的屏幕上,而不是作为 php 语句进行处理。

以下是我在上一个代码块中回显的变量之一的示例:

$precontent = "";
$precontent .= "

";
$precontent .= "";
$precontent .= "";
$precontent .= "
";
$precontent .= "
";
$precontent .= "
";
$precontent .= "
";

我尝试在字符串中嵌入另一个 php 语句,

$precontent .= ""; 这不起作用。

我也尝试过 ssi 格式,但 php 似乎忽略这些语句,因为它们是写在忽略语句内的。

任何帮助都会很棒。

I've been trying to clean up my site by defining my site structure as php string variables and then echoing those variables out. This condenses all of my site structure down to five lines with an article in between the echo statements. My page looks like this:

<?php
include('modules/moddefault.php'); //Link to site structure set as variables
$title = "Example Page"; // Define page title. This is inserted into the embedded title statement.

echo "$modtop"; // Declaration, head, title etc.
echo "$precontent"; // Pre-Content Structure
?>

<h1>Web Content Goes Here</h1>
<p>Article content will be nice and uncluttered.</p>

<?php
echo "$postcontent"; // Post-Content Structure
?>

I'm wondering how I can embed my CSS includes inside of a PHP string variable. PHP includes don't register because the php has already processed the page. The include statements are being printed to my screen instead of getting processed as a php statement.

Here is a sample of one of the variables I echo in the previous code block:

$precontent = "<body>";
$precontent .= "<div id='wrapper'>";
$precontent .= "<div id='header'>";
$precontent .= "<div>";
$precontent .= "include 'modules/header.inc'"; //THIS IS WHERE I'M HAVING TROUBLE
$precontent .= "</div>";
$precontent .= "</div>";
$precontent .= "<div id='navtop'>";
$precontent .= "<div>";
$precontent .= "include('modules/topnav.inc');"; // THIS IS WHERE I'M HAVING TROUBLE
$precontent .= "</div>";
$precontent .= "</div>";
$precontent .= "<div id='centerbox'>";
$precontent .= "<div id='article'>";
$precontent .= "<div id='innerarticle'>";
$precontent .= "<div>";

I tried embedding another php statement within the string,

$precontent .= "<?php include('modules/topnav.inc';?>";
This didn't work.

I have also tried ssi format, but php seems to ignore those statements because they are written inside an ignore statement.

Any help would be great.

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

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

发布评论

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

评论(2

我很OK 2024-10-24 05:37:37

您尝试过 $precontent.=file_get_contents('modules/header.inc');

have you tried $precontent.=file_get_contents('modules/header.inc');

缘字诀 2024-10-24 05:37:37

为什么不拥有类似的东西:

header.inc:

   <html>
   <head>
        <title><?php echo $page_title ?></title>
   </head>

   <body>
   [--- common page content here, e.g. menus ---]

然后

footer.inc:

   [---common page footer stuff here, eg. copyright lines ---]
   <p><?php echo $page_footer ?></p>
   </body>
   </html>

你就会有

article1.php:

<?php
    $page_title = "This is page 1";
    $page_footer = "Thanks for reading this";
    include_once('header.inc');
?>
<p>This is the content of page #1</p>
<?php
    include_once('footer.inc');
?>

Why not having something like:

header.inc:

   <html>
   <head>
        <title><?php echo $page_title ?></title>
   </head>

   <body>
   [--- common page content here, e.g. menus ---]

and

footer.inc:

   [---common page footer stuff here, eg. copyright lines ---]
   <p><?php echo $page_footer ?></p>
   </body>
   </html>

and then you'd have

article1.php:

<?php
    $page_title = "This is page 1";
    $page_footer = "Thanks for reading this";
    include_once('header.inc');
?>
<p>This is the content of page #1</p>
<?php
    include_once('footer.inc');
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文