Question:
Can you help me with the php include() and require() functions?
Kavrocks
2009-02-06 12:22:17 UTC
My website is currently made using html and css, I'm looking to convert parts of my website to use the php include() function to make editing my site easier.
I have the same navigation bar and banner on all of my pages.
I have my banner and navigation bar all in one table with css classes in a lot of the tags on it and there is a wide range of html tags. I want to know what do I do with this table to put it in the file I will include in all of my web pages with the include() function. Do I just copy and paste the table as it is starting with and ending with
or do I have to put it in php tags like this ?>? Is there any need for and in this file which I want to include in all of my pages?

In the pages where I want my include file to appear as normal do I just need to paste this: ?

I would be very grateful for your help as it will save me a lot of time. I've tried searching for the answer on the internet but I cant seem to find the answer.
Four answers:
anonymous
2009-02-06 12:33:04 UTC
Your include files will contain whatever you want: PHP, HTML, etc. This is not valid:




...


?>



That would be putting HTML code inside a PHP tag, you don't want to do that. For your included file, just put:



...your HTML stuff...






Then call that whatever you want, such as header.inc.php. Then in your main page:







require() is the same as include() except if the file is not found, the page will bomb out with an error, whereas include() will just keep on chugging away and MAYBE show a warning.



Just remember, whatever is in that include file gets automatically copied into your main page, no matter what's inside of it. So you're right, there is no need to put or in an include file. Just the exact code block you want to include at that point.



Hope that helps.



PS: Why would someone thumbs-down my response? It's totally correct and informative. What the heck?
softwareDev
2009-02-06 12:34:40 UTC
Let's say this is the html you want to include and it's saved as

"header.html"



Welcome to my site





if "header.html" does not have any php code in it, you do not have to save it with the extension .php or put it inside the tags



your're right on how to include the header file, if the page in which you are including it is in the same folder. If it's not then you have to specify the path relative to where your other file is.



If it's in the same folder use





If not in the same folder use







where the word "path" is the address of the header.html file
RokoR
2009-02-06 12:30:25 UTC
Put all the HTML content (...
) in one file and name it header.inc and then paste in all your pages where you want to display this header. Just remeber that your pages need to have .php ending for this to work.

Hope this helps..
anonymous
2016-10-05 01:04:04 UTC
do not use easily the direction with the rigidity letter, attempt the relative direction. if t.very own residing house page is interior a similar folder as try.very own residing house page, attempt require(t.very own residing house page) If try.very own residing house page is interior the htdocs folder, attempt require(/load/alley/t.very own residing house page) EDIT - final analysis, you won't be in a position to apply a rigidity letter interior the path.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...