New to PHP -- Want to create a function that calls header html

Perseus

Registered
Hello folks, I am new to PHP, and would like to create a PHP function that calls header html for a file that will be used across maybe 20 pages. (This is what PHP is for isn't it?) So does the file with html in it need to be a php file? I am hoping for something as simple as:
<?php get_header(); ?>

Thanks!!
 
Perseus said:
Hello folks, I am new to PHP, and would like to create a PHP function that calls header html for a file that will be used across maybe 20 pages. (This is what PHP is for isn't it?) So does the file with html in it need to be a php file? I am hoping for something as simple as:

Thanks!!

Use
Code:
<?php include 'header.php' ?>
to include the contents of the header.php file. If the header.php file resides in another directory, you could use a relative path in place of the file name.

You could write a function to do that, which would look like this:
Code:
<?php
    function get_header()
    {
        include 'header.php';
    }
?>

If you placed the function declaration in functions.php, for example, you would have to include the functions.php file in every file that calls get_header().

If you want to do anything fancier though, I urge you to take a look at Smarty to avoid falling in the embedded PHP trap, usually resulting in extremely complex and nearly unreadable code :)
The crash course is a very nice introduction — refer to the documentation for installation instructions.
 
You have to either name the page that has the funcion with a .php extension or in .htaccess file or the server config app(mac os X) for .html files to be servers as php.

I like to use the function then if you change the location/name of your header file you just have to change it once, in whre ever you set the function. ( i use an external page for all my function also and include it in to every page by default.
 
Back
Top