0

I have something like this:

<?php   $pages = array('home', 'docs', 'app', 'userg','appviz');
            if (!isset($_GET['_page']) || !in_array($_GET['_page'], $pages))
                list($_page) = $pages;
            else
                $_page = $_GET['_page'];

            include "pages/{$_page}.html";
        ?>

I want to include "pages/{$_page}.php" along with the .html. How do i do that?

4

1 回答 1

4

根据PHP 手册 include()

当包含一个文件时,解析会在目标文件的开头退出 PHP 模式并进入 HTML 模式,并在结尾处再次恢复。因此,目标文件中应作为 PHP 代码执行的任何代码都必须包含在有效的 PHP 开始和结束标记中。

由于include()需要一个 PHP 文件,您应该检查file_get_contents()注意:在 PHP 5 之后行为发生了变化。

$target_file = $_SERVER['DOCUMENT_ROOT']."/pages/$_page.html";
$html_file = file_get_contents($target_file, true);
echo $html_file;

如果您试图让 PHP 解析像 PHP 文件这样的 HTML 文档,那么您需要将指令添加到您的服务器配置或服务器.htaccess文件中:

AddType application/x-httpd-php .html .htm

然后你可以使用include(), include_once(),require()require_once().

于 2013-05-28T09:26:07.670 回答