Can you help me with the php include() and require() functions?

  • Thread starter Thread starter Kavrocks
  • Start date Start date
K

Kavrocks

Guest
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 <table> and ending with </table> or do I have to put it in php tags like this <?php <table> </table> ?>? Is there any need for <html><head> and <body> 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: <?php include("header.php"); ?>?

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.
Thank you for the replies, I now understand what needs to be done.
 
Your include files will contain whatever you want: PHP, HTML, etc. This is not valid:

<?php
<table> ... </table>
?>

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

<table> ...your HTML stuff...</table>
<?php ....php code if you want to...(optional).. ?>

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

<?php require("header.inc.php"); ?>

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 <html> or <body> 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?
 
Back
Top