Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to add a php include of the head section in my pages?

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.

like image 627
Suzi Larsen Avatar asked Oct 16 '12 13:10

Suzi Larsen


People also ask

What is include header PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Which function is used in PHP to include header file in a page?

The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent.

How do you include PHP section in HTML page?

We can insert any PHP file into the HTML code by using two keywords that are 'Include' and 'Require'. PHP include() function: This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called.


3 Answers

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>
like image 57
Shoe Avatar answered Oct 11 '22 20:10

Shoe


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 ...

like image 45
Ghasem Avatar answered Oct 11 '22 21:10

Ghasem


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

like image 27
Kobi Tate Avatar answered Oct 11 '22 21:10

Kobi Tate