# Solved: PHP Newbie questions



## skyhigh007 (Jun 17, 2004)

Hi

Here's my situation, I have created a website using Xhtml & CSS, the problem is that the header, footer and other constant contents are re-loaded in very page that I have created and I think that will slow the loading speed of the pages. So, I decide to try php where you can re-use the header, footer in every page and I don't have to go edit in every page. My question is which function in PHP should I use to re-use the header, footer , etc ? Include, require, include-once, require once ?

My files are: header.php, footer.php, ...etc

In index.php

<?php include( "header.php") ?>

middle content

<?php include("footer.php") ?>

is set up correct?

Thanks


----------



## Sequal7 (Apr 15, 2001)

It would be include, but don't forget the closing ;

```
<?php include(header.php); ?>
```
You could also use *require*, they perform the same function, just handle errors differently.


----------



## TheRobatron (Oct 25, 2007)

I always use 

```
<?php include "file.txt" ?>
```
because

```
<?php include(file.txt) ?>
```
never works for me...


----------



## MMJ (Oct 15, 2006)

Sequal7 said:


> It would be include, but don't forget the closing ;


Although thats good practice it isn't really necessary since you close the PHP block right after it.


----------



## skyhigh007 (Jun 17, 2004)

I have another question.

Do I have to put the following code in every php page, even in the header.php and footer.php?
Or I just have to put it in the individual main page?

"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Untitled Document

"


----------



## TheRobatron (Oct 25, 2007)

No, the include command inputs the text direclty into the document, so it's as if you actually wrote it in. Because PHP is server-side, the browser doesn't see any of the PHP and so just the text in the file. You still need to put in formatting tags though (

), unless you wrap them round the PHP on every page:

```
<?php include "file.php" ?>
```


----------



## skyhigh007 (Jun 17, 2004)

This is my footer.php


Home
Shipping&Return
Privacy Policy
Contact Us 
News
Site Map

Copyright © 2007. All Rights Reserved

Is this standard or I need to put some <?php ?> tag around the whole cde?


----------



## TheRobatron (Oct 25, 2007)

No, just write it exactly as if you were writing it directly into the HTML file. Remember, everything that is in the file will be put into the HTML file.


----------



## skyhigh007 (Jun 17, 2004)

Would you explain it more detail by giving examples that I use in the above code?


----------



## Sequal7 (Apr 15, 2001)

There are several ways of doing it, if you dont want to write the code for each page and don't mind a static page title, then redo your header like this:

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Website</title>
</head>
```
 Name that header.php

And redo your footer like this;

```
<div id="footer">
<ul id="foot">
<li><a href="index.php">Home</a></li>
<li><a href="shipReturn.php">Shipping&amp;Return</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.php">Contact Us </a></li>
<li><a href="news.php">News</a></li>
<li><a href="#">Site Map</a></li>
</ul>
<p class="copy"> Copyright &copy; 2007. All Rights Reserved </p>
</div></body>
</html>
```
 Name that footer.php

then your pages can look like this;

```
<?php include(header.php); ?>
<div class body>
<ul>
  <li>Bulleted list</li>
  <li>Bullett two</li>
</ul>
<ol>
  <li>Numbered lists</li>
  <li>second number</li>
  <li>third</li>
</ol>
<h3>Header 3</h3>
<h2>Header 2  </h2>
<h1>Header 1 </h1></div>
<?php include(footer.php); ?>
```
and the result would be;

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<div class body>
<ul>
  <li>Bulleted list</li>
  <li>Bullett two</li>
</ul>
<ol>
  <li>Numbered lists</li>
  <li>second number</li>
  <li>third</li>
</ol>
<h3>Header 3</h3>
<h2>Header 2  </h2>
<h1>Header 1 </h1></div>
<div id="footer">
<ul id="foot">
<li><a href="index.php">Home</a></li>
<li><a href="shipReturn.php">Shipping&amp;Return</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.php">Contact Us </a></li>
<li><a href="news.php">News</a></li>
<li><a href="#">Site Map</a></li>
</ul>
<p class="copy"> Copyright &copy; 2007. All Rights Reserved </p>
</div></body>
</html>
```
Don't forget to define your div body, this is just an example. If you don't define it in css then just add at the very first line to each page taht is including the head page.


----------



## skyhigh007 (Jun 17, 2004)

Hi

Thank you for the example. What do you mean when you said "if you dont want to write the code for each page and don't mind a static page title" ?


----------



## Sequal7 (Apr 15, 2001)

The page title would remain the same if you add the

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>[COLOR="Red"]My Website[/COLOR]</title>
</head>
```
Look at the page title here, it says "php Newby Question - Tech Support Guy Forums", then go to the forums, it says Tech Support Guy - Free help for ....."

You could define the page title displayed in the header page, with a php variable but you would need to define that on each page, adding something like;

```
<?
$subtitle = "your page title here"; //now include the header.php page
include('header.php');
?>
```
NOTE: Each page you are writing (other than the header and footer.php pages) will need this on it at the top. Change the "your page title here" to the page title you want on that particular page.

Then change your header page to this;

```
if (isset($subtitle)) {
   echo $subtitle;
} else {
   echo "default subtitle"; //change to a  default name if an error is discovered
}
```
Then change your title content in the header.php page to this;

```
<title>
   MAIN TITLE &raquo; <? if (isset($subtitle)) { echo $subtitle; } ?>
</title>
```
Full example of header.php

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?if (isset($subtitle)) {
   echo $subtitle;
} else {
   echo "default subtitle";
}?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
   Page Title &raquo; <? if (isset($subtitle)) { echo $subtitle; } ?>
</title>
</head>
```
This should give you defined page titles.


----------



## tomdkat (May 6, 2006)

skyhigh007 said:


> the problem is that the header, footer and other constant contents are re-loaded in very page that I have created and I think that will slow the loading speed of the pages.


How so?

Peace...


----------



## rick22 (Feb 28, 2008)

well, all of this just went up my head.... didnt understand even a single thing..


----------



## skyhigh007 (Jun 17, 2004)

Thanks for the example again. How do i or where should i declare a variable that can be used for many pages? Does it has to be on an individual page that is for global variables only ?

I hope all this reusing or sharing will speed up the loading process.


----------



## Sequal7 (Apr 15, 2001)

rick22 said:


> well, all of this just went up my head.... didnt understand even a single thing..


LOL, I know how you feel, sometime I confuse myself!



skyhigh007 said:


> How do i or where should i declare a variable that can be used for many pages? Does it has to be on an individual page that is for global variables only ?


See, not I am **....Sorry but I don't follow you on that question. By Variable for many pages do you mean a page title? or like a navigational bar that is included in the pages?


----------



## awatson (Jan 29, 2008)

yes, that's correct. Be sure the path to the file that's being included is correct.


----------



## skyhigh007 (Jun 17, 2004)

what I meant was to set a variable for content information in the body and you can use that variables everywhere.


----------



## Sequal7 (Apr 15, 2001)

Isn't that what your doing with the header and footers?


----------



## skyhigh007 (Jun 17, 2004)

Can you create an external sheet where you can defined all the global variables ?


----------



## rick22 (Feb 28, 2008)

yeah.. this is what i was about to say... is the path correct?


----------



## Jon1001 (Jun 28, 2007)

The difference between include and require is that if the include function cannot find the file, it still loads the rest of the page. If you use require and the path is incorrect, it will produce an error i.e. not show the rest of the page. In general I would say it is better to use require as you don't want to be loading parts of pages if bits are missing. I believe the include_once and require_once functions just mean that you can only include a file on a page one time, i.e. if the same file is included twice, the second will be ignored. Personally, I would just use require.


----------



## eddie5659 (Mar 19, 2001)

skyhigh007, I've merged both threads here. Please create one thread per problem 

eddie


----------



## skyhigh007 (Jun 17, 2004)

Sorry for creating two threads. I think my browser went dead and I refreshed it again, so that might be the problem. Thank you all for the support.


----------

