What is the best syntax for includes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

What is the best syntax for includes?

Post by loweauto »

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).
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: What is the best syntax for includes?

Post by roders »

Code: Select all

include('filename.html');
or

Code: Select all

include('/path/to/filename.html');
Thats it.
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: What is the best syntax for includes?

Post by solid »

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 :)
User avatar
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?

Post by AbraCadaver »

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:

Code: Select all

<?php include("head.html"); ?>
If I need something in a subdirectory I just do this:

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.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: What is the best syntax for includes?

Post by timWebUK »

AbraCadaver, could you explain your 'front controller' idea? Sounds interesting, but don't think I quite understand.
User avatar
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?

Post by flying_circus »

I have always used include_once('/full/path/to/file'). Is there a reason you guys use include()?
User avatar
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?

Post by AbraCadaver »

timWebUK wrote:AbraCadaver, could you explain your 'front controller' idea? Sounds interesting, but don't think I quite understand.
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.
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.
User avatar
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?

Post by AbraCadaver »

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;
}
include_one is very useful if your includes have function/class definitions because you'll get a fatal error.
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.
User avatar
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?

Post by flying_circus »

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. :)
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: What is the best syntax for includes?

Post by Technocrat »

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:

Code: Select all

include dirname(__FILE__).'/includes/file';
Post Reply