1

I'm pretty new to web development and I am trying to learn by trying to develop a website of course. However, I am hitting an issue, I don't get why my include function in PHP is not working.

My 'menu.php' file contains:

<?php
echo '
            <li><a href="#" class="current">Home</a></li>
            <li><a href="#">button1</a></li>
            <li><a href="#">button2</a></li>            
            <li><a href="#">button3</a></li>  
            <li><a href="#">button4</a></li> 
            <li><a href="#">button5</a></li> ';
    ?>

My html file contains the following:

...
<nav>
   <ul>
      <?php include 'menu.php'; ?>
   </ul>
</nav> 
...

What am I missing? If I copy the list items from my .php to my .html, it works properly. It works:

 ...
    <nav>
       <ul>
                <li><a href="#" class="current">Home</a></li>
                <li><a href="#">button1</a></li>
                <li><a href="#">button2</a></li>            
                <li><a href="#">button3</a></li>  
                <li><a href="#">button4</a></li> 
                <li><a href="#">button5</a></li> 
       </ul>
    </nav> 
 ...

Thank you in advance.

2
  • 1
    Your HTML file won't parse PHP unless it has a .php extension (or your server's been configured to parse .html files as PHP files). Commented Apr 16, 2014 at 16:36
  • 1
    What do you mean? My 'index.html' should be 'index.php' ? Commented Apr 16, 2014 at 16:37

2 Answers 2

3

When you say:

My html file contains the following:

Do you mean that you have a file like index.html? Because that means that PHP is not being parsed. All that's happening is that you have a weird <?php> element that gets silently ignored as invalid HTML.

Try renaming your file to index.php instead.

As an aside, you should always use the browser's View Source option before saying it doesn't do anything ;)

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

2 Comments

Exactly, I had index.html. This is exactly what I wanted to know. w3schools wasn't really specific about this matter. Thank you so much.
@MrSilent w3schools is a very, very bad reference. See w3fools.com. When learning PHP, the best reference is the Word of God.
1

You cannot put PHP content in an HTML file, it won't be processed as such. You should rename your .html page to .php.

Comments

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.