views:

281

answers:

9

I am wondering how I can break up my index.php homepage to multiple php pages (i.e. header.php, footer.php) and build a working index.php page using those separate php pages. I know WordPress uses this with different functions like:

GetHeader();
GetFoodter();

But when I tried to use those functions, it errors. I am guessing they are not native functions to PHP.

What would I need to do to get this functionality?

+8  A: 
include 'header.php';

include 'footer.php';
Scott
A: 

Use include statements to just include those files to your Page

I think it's

include '[filename]'
Tigraine
Actually, in PHP it is include('filename');
A: 

You could do the following:

<?php
    include('header.php');
    // Template Processing Code
    include('footer.php');
?>
Noah Goodrich
+2  A: 

Go with an MVC framework like Zend's. That way you'll keep more maintainable code.

Jilles
Although Zend is rather heavyweight, it's an option. I would recommend looking at frameworks, however, such as CakePHP, symfony, and CodeIgniter.
Thomas Owens
Awesome, thanks for the recommendation. This isn't really that big of a site so I think the above [include 'file-name.php';] is going to be good enough. I will definitely have to read up on CakePHP and Zend.
JoshFinnie
I think that would be a good thing (and as Thomas suggests, don't limit yourself to Zend). If I had a dime for each time I thought "this is just going to be a little site..." :-P
Jilles
+1  A: 

The include() statement includes and evaluates the specified file.

so if you create index.php as:

<?php
include("1.php"); include("2.php"); include("3.php");
?>

processing it will combine three php files (result of parsing them by php) into output of your index.php ... check more at http://pl.php.net/manual/pl/function.include.php

belunch
A: 

If your server is configured accordingly, you can use PHP's built in auto append/prepend settings and set it in a .htaccess file:

php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"

Info:
www.php.net/manual/en/configuration.changes.php#configuration.changes.apache
www.php.net/ini.core#ini.auto-prepend-file
www.php.net/ini.core#ini.auto-append-file

+1  A: 

Also, if i recall correctly, you can also use

<?php
require('filename');
?>

the difference being, if php can't find the file you want to include, it will stop right there instead of keep excecuting the script...

EroSan
A: 

I realize this is an old question, which already has a perfectly valid accepted answer, but I wanted to add a little more information.

While include 'file.php'; is fine on it's own, there are benefits to wrapping these sorts of things up in functions, such as providing scope.

I'm somewhat new to PHP, so last night I was playing with breaking things into files such as 'header.php', 'footer.php', 'menu.php' for the first time.

One issue I had was that I wanted to have the menu item for a page/section highlighted differently when you were on that page or in that section. I.e. the same way 'Questions' is highlighted in orange on this page on StackOverflow. I could define a variable on each page which would be used in the include, but this made the variable sort of global. If you wrap the include in a function, you can define variables with local scope to handle it.

AgentConundrum
A: 

You could also look into a template engine like Smarty. That way you define the the header and footer and all other common elements in a single file, then fill in the rest through smaller templates or direct output.

Chrisbloom7