Pk King X11
Pk King X11

Reputation: 163

How to stitch multiple parts of a PHP page together?

OK, so what I'm trying to do here is minimise data repetition and saving my self having to change every page. I create two files to be used as templates on all other pages. Those files will be the page top and bottom (header and footer), the bit in-between will be the actual pages. the header will have the head element and something in the body tag such as the navbar and footer will have a actual footer and end the body tag and html.

So what I tried to do so far was something like this:

<?php Echo file_get_contents('/templates/header.txt'); ?>
<p>Blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah</p>
<?php Echo file_get_contents('templates/footer.txt'); ?>

This works fine if header and footer are in html but if they contain php code it will not work since you cant echo php code. So my question is how can I do that but have it working with php code.

Edit: I also tried include but that does not work either.

I have a working php page with lots of php goin on at top and bottom. I want to cut up the top and bottom into two files that I can switch up:

header.php

footer.php

and my all my test page has is this (test.php):

<?php include('/templates/header.php'); ?>
        <p style="color:lime;">Logged in as <?php echo htmlentities($_SESSION['username']); ?> (<a href="includes/logout.php">Log out</a>)</p>
        <video width="853" height="480" controls>
            <source src="/videos/BIRDS OF PREY 4K (ULTRA HD).mp4" type="video/mp4">
            Your browser does not support the video tag.
        </video>
        <p>4K test video, watch in full screen! :)</p>
<?php include(/templates/footer.php); ?>

It simply stops working, but if I put it back together it works.

Upvotes: 0

Views: 326

Answers (1)

Scott Saunders
Scott Saunders

Reputation: 30394

<?php include('/templates/header.php'); ?>

It can have the php extension even if the file just contains HTML or text.

Upvotes: 3

Related Questions