PHP - 包含文件并执行其中的代码

PHP - Include file and execute code in it

我有这个代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title><?php echo $title; ?></title>
</head>
<body>
    <?php foreach ($posts as $post) { ?>
        <div class="post">
            <h3 class="title"><?php $post["title"]; ?></h3>
            <p class="text"><?php $post["text"]; ?></p>
        </div>
    <?php } ?>
</body>
</html>

我需要将它包含在其他文件中并执行其中的所有 PHP 代码。我该怎么做?

使用 include() 函数。

include("the file.php")

包含绝对路径:

<?php include($_SERVER["DOCUMENT_ROOT"].'/yourdirectory/yourfile.php'); ?>