8

I am creating my portfolio site and I am wanting to include the head section as a php include on my page. Reason being is because the site will have a fair few pages and I will want to make changes later on to things later on like tidying up the css files.

For example;

<head>
    <?php include('head.php'); ?>
</head>

as opposed to all this below being shown on each and every page:

    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="css/1140.css">
    <link rel="stylesheet" href="css/ie.css">
    <script src="js/vendor/modernizr-2.6.1.min.js"></script>
</head>

I just didn't know if this was good practice to do, as with this being my portfolio site, I need the code to be correct from the start also as they will probably look into the standard of it also.

What are your opinions and advice people? Thanks.

Suzi Larsen
  • 1,420
  • 5
  • 18
  • 32
  • 3
    It's a very common practice to use PHP for templating. Do the same with the footer. Lots of people start out by placing all that content into every static page. The next progression is to realize the inefficiency and replace it with an included file (where you are now). Your next step after this may be to move toward some kind of content management system. – Michael Berkowski Oct 16 '12 at 13:59
  • I mean, that works. But not if you're doing it for every file. You should look into templating. – wesside Oct 16 '12 at 14:06
  • It is always good practice to provide reusable and modifiable parts in separate scripts. You may even use some of these in multiple web sites with a clever configuration / structure. – Ertunç Oct 16 '12 at 14:06

4 Answers4

16

Yep, it's quite standard. But instead of writing:

<head>
    <?php include('head.php'); ?>
</head>

you should put the tags inside head.php. I say it's better because what's inside head.php has no sense without the head tags, so they are kinda linked together. It's good practice to join things so linked into a single file without having to repeat open and close head tags for each page.

Actually, it's even good practice (and commonly used) to have header.php, body.php and footer.php files that has respectively:

header.php

<html>
<head>
...
</head>
<body>

body.php

...

footer.php

</body>
</html>
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 1
    thanks Jeffrey, that is a good point. I will include the tags in the file. – Suzi Larsen Oct 16 '12 at 16:26
  • 1
    But if you put all the head in the external file you will have to put the title and description of the page, and this must be different in each page. Am I missing anything? – Nrc Mar 20 '14 at 12:57
  • 2
    @Nrc, just have a `$title` variable in `header.php` and changed it per page. – Shoe Mar 20 '14 at 13:10
  • 1
    That's a great idea. Can you please also tell what would index.php(where we include these files) look like? – user3834119 Jun 16 '15 at 11:21
6

I'm doing that in my application but I've found that it's not a good idea, because you have many of your stylesheets, javascripts, etc in a php file including the head section and you'll have problems with including it in php files in nested folders. this problem is because of relative paths. If you can use absolute paths then it's ok otherwise it's not a good idea ...

Ghasem
  • 79
  • 4
  • 2
    I think there is a way round that, It seems to work for me. However there may be issues with doing that which I am not aware of? – Suzi Larsen Oct 16 '12 at 16:11
  • It's not taking back to the root to include the head.php or css file, it's just relative to the current script location. (I'm talking about windows, not sure about linux environment) You can use this method in small applications, but not a good idea for big applications. As bigman said, templating and MVC seems to be good for large apps – Ghasem Oct 19 '12 at 17:37
4

PHP Includes are used like this all the time. Any time that you have content that will be the exact same on every page, it is very helpful to use an include

Kobi Tate
  • 373
  • 3
  • 18
2

This is an old topic but I use

    <?php include_once("phpinclude/head.txt"); ?>

phpinclude is it's own folder and I keep the footer, header, and common place info in that folder. .js, and .css has it's own as well.

Edit: I use require now. I would rather have a code fail and die rather than give some random string. They are the same except one dies and the other will print out an error or random code. This is for people learning PHP, not old heads.

Merlin8771
  • 369
  • 1
  • 5