1

I am trying to use the php include tag for my header and footer, and can't seem to find the problem. My include code is fine in my index file, and my header.php file shows no errors, but t doesn't include the file when I preview it in a browser. Is this an easy fix or am I missing something.

My index.php file include snippet

<div id="container">
   <?php include('include/header.php');?>
<div id="body">

My header.php file code

<?php 
echo <<<END
<header>
<nav>
<li onclick="location.href='index.html';"  style="cursor:pointer;"><a href="index.html" class="active">Home</a></li>
<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Contact</a></li>
<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">FAQ's</a></li>
<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Product Solutions</a></li>
<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Services</a></li>
<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Request a Quote</a></li>
</nav>
</header>
END;
?>
8
  • So what is your browser displaying instead? Commented Aug 4, 2014 at 17:19
  • my browser shows the rest of the index.php file, just without the header section Commented Aug 4, 2014 at 17:20
  • 3
    It looks like you forgot to escape a single quote in FAQ's. Also, why use echo in this case when you could just close the PHP tag? Large blocks of static HTML don't have to be outputted using echo. Commented Aug 4, 2014 at 17:21
  • 1
    @Jason There's no quoting problems, StackOverflow just doesn't understand heredoc syntax. Commented Aug 4, 2014 at 17:24
  • 1
    @MrLore apologies, I'm not familiar with that syntax. You may be correct. I just noticed it because it stood out on the SO syntax highlighter. Commented Aug 4, 2014 at 17:35

4 Answers 4

2

It seems ok on my computer. Are you sure that's your header file is the right directory? It should be:

---- index.php
---- include (directory)
     |---- header.php

If it's ok you must file permissions and be sure that index can include the header file.

Why do you need to use heredoc syntax?

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

2 Comments

This isn't much of an answer. It appears it would be better suited as a comment. Just saying "it works for me" isn't going to solve anything.
tested my site on my host server and the files showed up fine, error setting up test server i guess
0

Why not do this instead:

<?php
    ... some code ...
?>

<header>
    <nav>
        <li onclick="location.href='index.html';"  style="cursor:pointer;"><a href="index.html" class="active">Home</a></li>
        <li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Contact</a></li>
        <li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">FAQ's</a></li>
        <li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Product Solutions</a></li>
        <li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Services</a></li>
        <li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">Request a Quote</a></li>
    </nav>
</header>

<?php ... more code ... ?>

<!-- more html -->

You don't need echo. Any HTML not within tags are automatically outputted anyways. Remember that PHP is based on HTML. You can think of PHP as HTML with <?php?> tags mixed in.

6 Comments

is this all in the index.php or header.php
@Gibblefish it can be done in either. If you want to output something dynamic, like a variable string inside one of the anchor tags, you can reopen the PHP tags like: <li onclick="location.href='#';" style="cursor:pointer;"><a href="#.html"><?php echo $customLinkText; ?></a></li>.
problem with your answer, the php line in my index is surrounded by html, not the other way around
@Gibblefish I'm not sure what you mean by that, but you can use as many <?php ?> tags as you'd like. I'll edit the answer to demonstrate.
@Gibblefish I edited my answer. As you can see, you can use many PHP tags. You can even place PHP tags inside of other tags: <a href="<?php echo "URL"; ?>"></a>. Where you place PHP tags doesn't matter.
|
0

Change this line:

<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">FAQ's</a></li>

With this:

<li onclick="location.href='#';"  style="cursor:pointer;"><a href="#.html">FAQ\'s</a></li>

The error was this:

FAQ's

And I changed it to this:

FAQ\'s

4 Comments

-1, This is incorrect, all your change does is change FAQ's to FAQ\'s in the output. All quotation marks are completely ignored inside a heredoc block.
This is not the answer for his question,But its another error in his code.
There is no error though, if you run his code it works as expected because quotation marks have no meaning inside a heredoc and never need to be escaped. Test it on Ideone.
@MrLore running it on Ideone, does that mean it works and there is something on my end test server wise that isn't set up correctly? I using xxamp for non published sites
-1

what do you mean by

echo <<<END ???

however the file header.php is in a separate folder ? look at in the console to see if it can locate the file or not... for me i used to work with :

require_once("include/header.php");

2 Comments

This would be better suited as a comment, as you shouldn't be asking questions in answers and there's no reason require_once would work if include doesn't.
i can't comment , and i just trie to give you an alternative that's all.good luck

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.