包括'count.html.php'

这本书'php and mysql novice to ninja'说要包含文件count.html和count.php,我必须在count.php文件的底部键入以下内容:


include 'count.html.php';

但是,这似乎不起作用,除非它只是没有 .php 的 count.html。


这本书到底对不对?我在网上找不到任何关于这种包含方式的信息。


我已经看到了两条单独的包含标签的行,例如:


include 'a.html';

include 'b.php';

对不起,狗屎格式。我以前从来没有在这个网站上发过帖子。


计数.php:


<?php

$output = '';

for ($count = 1; $count <= 10; $count++) {

        $output .= $count . ' ';                 

}


include 'count.html.php';

?>

计数.html:


<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>counting to ten</title>          

    </head>

    <body>

        <p>

            <?php echo $output; ?>


        </p>

    </body>

</html>


慕运维8079593
浏览 96回答 1
1回答

一只斗牛犬

include查找与您在引号中的内容完全匹配的文件名,它不会像您描述的那样一次包含两个不同的文件。你的count.html文件中包含 PHP 代码,所以它不应该有.html扩展名,它会最常用,.php或者因为它看起来很像一个 html 模板文件,你可以使用.phtml. 扩展名.php和.phtml做完全相同的事情,但通常按照惯例,如果你有一个专门包含 php 代码的文件,你会命名它.php,它有很多 html,你可能想命名它.phtml。两者都可以,但不是平原.html。假设您决定命名它count.phtml,那么您的两个文件将如下所示:计数.php<?php$output = '';for ($count = 1; $count <= 10; $count++) {&nbsp; &nbsp; &nbsp; &nbsp; $output .= $count . ' ';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}include 'count.phtml';?>计数.phtml<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; &nbsp; &nbsp; <title>counting to ten</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php echo $output; ?>&nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP