1

I am new to web development and am starting to learn php. I am wanting to make a website and am just working on small projects to learn different approaches.

I created a php file that contains the html information for the navbar so I can just include the php file and won't have to update every page that will have the navbar.

Here is my html file.

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="home.css">
        <title id='title'>Homepage</title>
    </head>
    <body>

    <?php include("menu.php"); ?>

    <h1>Welcome to the homepage.</h1>
    <h2>
        <?php
            echo "php is working";
        ?>
    </h2>
    <p>I will soon be update the page to look much nicer.</p>
    </body>
</html>

Here is my php file.

<?php
    echo <<< EOT
    <div class="navbar navbar-static-top">
        <ul class="nav">
            <li class="home"><a href="index.html">Home</a></li>
            <li class="about"><a href="#">About</a></li>
            <li class="projects"><a href="#">Projects</a></li>
            <li class="resume"><a href="#">Resume</a></li>
        </ul>
    </div>
    EOT;

?>

When I open up the webpage none of it is working. But if I remove the php script that has the html and just insert the html in its place it works. I know php is installed correctly becuase the php script in h2 at the bottom of the file works.

3
  • 2
    so is this html loaded on your server from a .html file? or .php? .html files by default are NOT treated as PHP code and will NOT be run through the php interpreter. if you do a "view source" on your page, you'll probably see the raw php code in there. Commented Feb 10, 2014 at 1:32
  • they are both saved as .php Commented Feb 11, 2014 at 2:42
  • hey did u ever figure out the issue? Commented Jun 9, 2014 at 4:27

3 Answers 3

3

HTML files will not be read for PHP. And the include statment needs to be read as PHP. If your file has any PHP inside it, it must be a .php file.

Sign up to request clarification or add additional context in comments.

1 Comment

Colby is right. Just rename your .html file to .php and it will work.
2

I know this is late, but this is your problem:

The EOT at the end of your menu.php file cannot be indented if you want it to work.

From the documentation:

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.

In addition, you have a space between your <<< and your EOT, though I'm not 100% sure if that will cause a problem. If you change your code to this, it should work:

<?php
    echo <<<EOT
    <div class="navbar navbar-static-top">
        <ul class="nav">
            <li class="home"><a href="index.html">Home</a></li>
            <li class="about"><a href="#">About</a></li>
            <li class="projects"><a href="#">Projects</a></li>
            <li class="resume"><a href="#">Resume</a></li>
        </ul>
    </div>
EOT;

?>

Comments

-2

The include doesn't require brackets. Make it...

 <?php include 'menu.php'; ?>

Also make sure both files are saved as .php files as .html files won't correctly include something. It may be your browser be clever that is making the tags work.

Hope this helps :)

3 Comments

Yea include/require doesnt need brackets, but adding/removing them doesnt make any sense...
This will quite definitely resolve his error. Reading the entire answer..
Which one exactly? removing brackets or fixing file extension...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.