Jump to content

Yet another PHP include problem...


high_road

Recommended Posts

Yes, I can see this is a common problem addressed here. And I've read through numerous posts trying to figure out why my own simple PHP include isn't working. No luck so far...

 

I have some experience working with PHP scripts and apps, but really not a good grasp of it, so bear with me. Here's the include statement I'm using:

 

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

 

The includes directory is located in the httpdocs directory, along with the site's index.php and other top level files.

 

Here's the code for the header.php file I'm trying to include:

<?php
  <div id="header"> <a href="index.html"><img src="media/logo.jpg" alt="" name="logo" width="246" height="104" border="0" id="logo" /></a> 
    <img src="media/header_banner.jpg" alt="" name="header_banner" width="500" height="104" id="header_banner" /> 
    <br class="clearfloat" />
<!-- end #header -->
  </div>
?>

 

It's been several years since I used a PHP include (have had a hiatus from real hands on webdev work for the most part), but I never had this much trouble with it. I've read a number of PHP includes tutorials online today, and have seen different variations on how this is done... Have tried them all with no success. (Can anyone explain why some folks have the file location enclosed in double quotes rather than single quotes? I even see that in posts in this forum. Either way, this doesn't work for me.)

 

All the rest of the page loads, but no header HTML. I'm wondering if this is some peculiar server configuration thing. This should be stupid-simple to do, and I'm tired of tearing my hair out over it. But I NEED to be able to use includes.

 

Any help appreciated...

 

Thanks,

 

Steve

 

Link to comment
Share on other sites

The reason is your opening tags for PHP. If you notice, you have zero PHP within the <?php ?>, only HTML.

 

So why did you open the tags for? Only because your file extension is .php it doesn't mean it needs to contain PHP only. If you would like to keep the tags open then you would need to do something like

 

<?php

echo <<<HTML
<!--NOW YOU CAN INPUT ALL THE HTML YOU WANT HERE -->
HTML;
?>

 

But why parse trash through the PHP engine when you can avoid that? ;)

Link to comment
Share on other sites

Or just save the include as an html file. It works just as well.

 

I would actually keep it as .php because if in the future the developer decides to add some PHP to the header, hes now going to have to go through all the files that have include set to .html extension and change it back to .php.

 

Gotta think 3 steps ahead ;)

Link to comment
Share on other sites

Thanks, guys, for the reply. Yes, it shows... I'm really rusty... That's what I get for following tutorials literally. (Would've helped if they had pointed out that straight HTML doesn't need to be wrapped in <?php ?> ... but then again, it goes to show how much I still have to learn.)

 

Now it works. Bingo!

 

Mil gracias!!

 

Steve

 

PS: Yeah, maybe I ought to dust off that old Sitepoint build-a-website-with-PHP-and-MySQL book...

 

;-)

Link to comment
Share on other sites

Yes, I saw that. Thanks for the reminder! I need to snap up some books asap. (Have my eye on two CSS books...)

 

I have version 1 of the PHP / MySQL book. Wonder if that's sufficient to gain some rudimentary, basic knowledge... or has PHP really changed that much? (I would only be doing very basic stuff with PHP for sure, for the near-medium future.)

 

Thanks again...

 

Steve

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.