Web Hosting Talk







View Full Version : Using php include statement to re-use code


AC1
09-20-2002, 05:35 AM
Hi

Every time I make a change to my standard page header, footer etc, I had to repeat it once per screen. Which was proving very labour intensive.

I have now utilised the PHP include command to include standard headers and footers in all my pages so that I only have to make the change once.

Because of the php code now in my home page I have to call it index.php. It seems to work OK when I tested it.

Is this the correct way to reuse code in PHP and will there be any problems calling my home page index.php?

Any feedback on this would be appreciated.

Cheers
Andy

Myth_Pharoah
09-20-2002, 06:37 AM
I don't see anything wrong with it. If you've just used the include statements it should be fine. May be you can show us your code and we can help better then?

AC1
09-20-2002, 07:26 AM
Hi

My code is simply:

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

All the main code for page is included here

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

</html>

Samuel
09-20-2002, 07:35 AM
Originally posted by AC1
Hi

My code is simply:

<html>
<?PHP include ("header.php"); ?>

All the main code for page is included here

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

</html>

Rich2k
09-20-2002, 08:20 AM
I used to do it like that but now I tend to use classes... as I use a number of global functions across the sites.

BTW you don't have to include PHP in <?PHP

Especially if you have short_tags enabled (which is by default).

net-trend
09-20-2002, 09:04 AM
<?PHP include ("header.php"); ?>

the PHP after the ? is not needed. Although it is recommended and good practise to always use it.

BTW, what you are trying to do it using templates. It will work the way listed. :)

AC1
09-20-2002, 09:09 AM
Hi

Thanks for all the comments, I just wanted to make sure I was not going down the wrong path with this.

Cheers
Andy

Rich2k
09-20-2002, 09:49 AM
Yes I only use classes as I tend to use a lot of XML and XSLT and it makes it easier for the XML caching

Myth_Pharoah
09-20-2002, 02:45 PM
Why not put the HTML start and end tags in the header and footer files as well?

dreamrae.com
09-20-2002, 09:01 PM
you dont do this already? the include statement is the lazy programers friend :D