What is the best syntax for includes?
Moderator: General Moderators
What is the best syntax for includes?
Hello, I'm trying to build my site with using includes, as to have separate files to be included for the head, navigation bar, footer, etc.
What is the best syntax, or form of include, or way of writing for this purpose?
Just to have index.html have various includes to files such as head.html, nav.html, footer.html etc.
Currently I'm using the following line everywhere I want to do this:
<?php include($_SERVER['DOCUMENT_ROOT']."/head.html"); ?>
Would you advise doing otherwise - written in a different form? Can you see any problems with this?
FYI: The index.html is just a regular file, containing info about the site (and of course I've turned on the function in htaccess so that php can be executed in html files), and all the files I'm including also have regular HTML, some will have some php, and Ajax (for forms), and one will be a blog page (probably with Wordpress).
What is the best syntax, or form of include, or way of writing for this purpose?
Just to have index.html have various includes to files such as head.html, nav.html, footer.html etc.
Currently I'm using the following line everywhere I want to do this:
<?php include($_SERVER['DOCUMENT_ROOT']."/head.html"); ?>
Would you advise doing otherwise - written in a different form? Can you see any problems with this?
FYI: The index.html is just a regular file, containing info about the site (and of course I've turned on the function in htaccess so that php can be executed in html files), and all the files I'm including also have regular HTML, some will have some php, and Ajax (for forms), and one will be a blog page (probably with Wordpress).
Re: What is the best syntax for includes?
Code: Select all
include('filename.html');Code: Select all
include('/path/to/filename.html');Re: What is the best syntax for includes?
What you are doing is good. If you want to split hairs, the only way that I see to make your usage better would be to use single quotes instead of double quotes, as single quotes are always preferable (faster) when there is no rendering to be done (not using things like \n, etc.), but this is being very nit-picky 
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: What is the best syntax for includes?
It depends upon how your directory structure is laid out. I use a front controller in the root directory that includes other pages so my includes are always relative to the root which lets me do this:
If I need something in a subdirectory I just do this:
Code: Select all
<?php include("head.html"); ?>Code: Select all
<?php include("includes/head.html"); ?>mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: What is the best syntax for includes?
AbraCadaver, could you explain your 'front controller' idea? Sounds interesting, but don't think I quite understand.
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: What is the best syntax for includes?
I have always used include_once('/full/path/to/file'). Is there a reason you guys use include()?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: What is the best syntax for includes?
Well, either in an MVC system or not, I use for example index.php in the root directory. index.php is always loaded and it includes what else is needed, starts up the session, filters input, etc., determines the layout and takes action based on get or post vars. So if index.php?type=blog&op=view&id=1 then the controller would do a bunch of common things by including files, load up/include the blog files, call a blog function or method and the blog would use the id to load up the correct blog item. Of course in the code or with rewrite rules you can use /blog/view/1 instead. This is probably how you have seen many open source applications designed.timWebUK wrote:AbraCadaver, could you explain your 'front controller' idea? Sounds interesting, but don't think I quite understand.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: What is the best syntax for includes?
flying_circus wrote:I have always used include_once('/full/path/to/file'). Is there a reason you guys use include()?
If you want to include the file more than once, then include_once won't do it. Very basic and maybe not real world, but consider the following:
Code: Select all
// index.php
$msg_type = 1;
include_once('messages.php');
// lots of random PHP and HTML
$msg_type = 2;
include_once('messages.php');Code: Select all
// message.php
switch($msg_type) {
case 1:
// do something
echo 'Something';
break;
case 2:
// do something different
echo 'Something different';
break;
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: What is the best syntax for includes?
Sure, I'm with ya. I just wasnt seeing a practical reason to include "the head, navigation bar, footer, etc." more than once, which is how I interpreted to OP. 
- Technocrat
- Forum Contributor
- Posts: 127
- Joined: Thu Oct 20, 2005 7:01 pm
Re: What is the best syntax for includes?
Using include_once or require_once has some more overhead and slight speed loss. You should really only use those if you think there is a possibility of more than one inclusion.
Also you should really use the direct (or full) path (ie include('/var/www/site/includes/file')) and not the partial path (ie include('/includes/file')) as again there is a slight speed loss as you are having the compiler search for the file in the paths and not just directly linking to it. There is also a potential issue if a file tries to include a base file from deep in a the file structure. It can lead to a file not found. So it's a good idea to do use the direct path like you are. You can also do:
Also you should really use the direct (or full) path (ie include('/var/www/site/includes/file')) and not the partial path (ie include('/includes/file')) as again there is a slight speed loss as you are having the compiler search for the file in the paths and not just directly linking to it. There is also a potential issue if a file tries to include a base file from deep in a the file structure. It can lead to a file not found. So it's a good idea to do use the direct path like you are. You can also do:
Code: Select all
include dirname(__FILE__).'/includes/file';