Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Creating the Menu and Footer Including the Header

Christopher Davis
Christopher Davis
10,399 Points

Why won't include statements wont work when header is in "inc" file?

I used the include command from the video "Including the header" on the PHP track. I included my files from the inc file and the CSS was broken. When I moved the header file to the main "Shirts 4 Mike" directory, everything functioned as it should. I tried changing the file path on the CSS link reference to go up one file "../css/style.css" but still it did not work. Has anyone else encountered issues with putting the header file in the inc folder? In the mean time, I am going to use the work around of moving all the files to the main folder and driving on with the project. It's just bugging me that I can't figure it out

It's hard to know for sure without seeing your code. What I can say is that using include(<file>) will copy the contents of the file into the location of the include line. So, when choosing a relative path, it's from the file with the include, not from the file being included.

<?php
// index.php example

// starting from the current directory, this will look in the inc folder for header.php
// and copy the contents into this spot
include "inc/header.php";

// let's say this link to a css file came from header.php.
// the href below is relative from this current directory, not from where header.php originated
<link href="css/style.css" rel="stylesheet">
?>

2 Answers

Ernest Rodriguez
Ernest Rodriguez
17,643 Points

you need to have parenthesis around the file path.

Christopher Davis
Christopher Davis
10,399 Points

I had two header.php files and I was changing one and not the other. Thanks for the answers. I feel like a dummy right now.