Search code examples
phphtmlinclude

Include 'count.html.php'


This book 'php and mysql novice to ninja' said that to include the files count.html and count.php, I must type the following into the bottom of the count.php file:

include 'count.html.php';

However, this does not seem to work unless it is just count.html without the .php.

Is this book accurate or not? I cannot find anything online about this way of including.

I've seen two separate lines of including tags such as:

include 'a.html';
include 'b.php';

Sorry for the shit formatting. I've never posted to this site before.

count.php:

<?php
$output = '';
for ($count = 1; $count <= 10; $count++) {
        $output .= $count . ' ';                 
}

include 'count.html.php';
?>

count.html:

<!DOCTYPE html>
<html lang="eng">
    <head>
        <meta charset="utf-8">
        <title>counting to ten</title>          
    </head>
    <body>
        <p>
            <?php echo $output; ?>

        </p>
    </body>
</html>

Solution

  • include looks for a file name exactly matching what you put in the quotes, it will not include two different files at once as you are describing.

    Your count.html file contains PHP code in it, so it shouldn't have the .html extension, it would most commonly use .php or since it looks mostly like an html template file, you could use .phtml. The extensions .php and .phtml do the exact same thing, but generally by convention if you have a file exclusively with php code, you would name it .php, and it it has a lot of html, you might want to name it .phtml. Either is fine, but not the plain .html.

    Assuming you decided to name it count.phtml, then your two files would then look like:

    count.php

    <?php
    $output = '';
    for ($count = 1; $count <= 10; $count++) {
            $output .= $count . ' ';                 
    }
    
    include 'count.phtml';
    ?>
    

    count.phtml

    <!DOCTYPE html>
    <html lang="eng">
        <head>
            <meta charset="utf-8">
            <title>counting to ten</title>          
        </head>
        <body>
            <p>
                <?php echo $output; ?>
            </p>
        </body>
    </html>