high_road Posted February 12, 2009 Share Posted February 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
Clinton Posted February 12, 2009 Share Posted February 12, 2009 Could it be the location of your img src="media/header_banner.jpg"??? Does it parse the include where it's at, i.e. in the include folder? If it does then might it need to change to ../media/blah.jpg? Quote Link to comment Share on other sites More sharing options...
Clinton Posted February 12, 2009 Share Posted February 12, 2009 I take that back... after looking at my includes. Let me look again. Quote Link to comment Share on other sites More sharing options...
Clinton Posted February 12, 2009 Share Posted February 12, 2009 How about taking out the <?php ?>. None of that is PHP. Quote Link to comment Share on other sites More sharing options...
9three Posted February 12, 2009 Share Posted February 12, 2009 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? Quote Link to comment Share on other sites More sharing options...
Clinton Posted February 12, 2009 Share Posted February 12, 2009 Or just save the include as an html file. It works just as well. Quote Link to comment Share on other sites More sharing options...
9three Posted February 12, 2009 Share Posted February 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
Clinton Posted February 12, 2009 Share Posted February 12, 2009 Ok, you win. :-) Quote Link to comment Share on other sites More sharing options...
high_road Posted February 12, 2009 Author Share Posted February 12, 2009 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... ;-) Quote Link to comment Share on other sites More sharing options...
9three Posted February 12, 2009 Share Posted February 12, 2009 I believe they released a version 2, not sure. But they have a special right now by 5 books for 29.99 USD in PDF formats. Quote Link to comment Share on other sites More sharing options...
high_road Posted February 12, 2009 Author Share Posted February 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
9three Posted February 12, 2009 Share Posted February 12, 2009 I'm not sure what php version the first version of sitepoint book used but if they used php 4 you might want to pick up php 5 for better support. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.