Reputation: 4911
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?
Upvotes: 2
Views: 1041
Reputation: 3564
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.
Upvotes: 0
Reputation:
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.
Upvotes: 0
Reputation: 339
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...
Upvotes: 1
Reputation:
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
Upvotes: 0
Reputation: 131
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
Upvotes: 1
Reputation: 748
Go with an MVC framework like Zend's. That way you'll keep more maintainable code.
Upvotes: 2
Reputation: 25263
You could do the following:
<?php
include('header.php');
// Template Processing Code
include('footer.php');
?>
Upvotes: 1
Reputation: 23668
Use include statements to just include those files to your Page
I think it's
include '[filename]'
Upvotes: -1