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

后端 未结 4 921
我在风中等你
我在风中等你 2021-02-06 11:25

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

相关标签:
4条回答
  • 2021-02-06 12:05

    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>
    
    0 讨论(0)
  • 2021-02-06 12:05

    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

    0 讨论(0)
  • 2021-02-06 12:10

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

    0 讨论(0)
  • 2021-02-06 12:24

    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.

    0 讨论(0)
提交回复
热议问题