0

Possible Duplicate:
When should I use require_once vs include?

For example if I have:

header part(header.php)
footer part(footer.php)
list of functions (functions.php)
list of constants (constants.php)
connect to database part(connection.php)
footer part + close the connection part(footer.php)

In these example should I use include, require or require_once and please NOTE why?

Community
  • 1
  • 1
user1349313
  • 143
  • 2
  • 7

2 Answers2

8

For files that contain functions, classes and other utilities, you generally want require_once so that nothing is accidentally redeclared in multiple libraries (or something) and breaks stuff.

  • functions.php
  • constants.php
  • connection.php

For files that are templates you usually want require, because they should be able to be included multiple times without causing issues. (Not that that's likely in the particular case of these two files.)

  • header.php
  • footer.php

You never* want to use include (or include_once). It's like require, but only shows a warning when the file doesn't exist - probably not what was intended.

Now you noted that your footer closes the database connection. You generally want to avoid side-effects in template files. Moreover, closing the database connection is probably unnecessary. (And given that you can close it, here's some advice: use PDO instead!)

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Why redaclaring functions bad if that function is the same function? – user1349313 Jul 30 '12 at 01:16
  • 1
    @user1349313: 1. It would be pointless. 2. It causes an error. – Ry- Jul 30 '12 at 01:17
  • yes I understand its pointless but how can it cause an error if the functionality of that function didnt change? – user1349313 Jul 30 '12 at 01:19
  • @user1349313: Because function redeclaration is illegal in PHP. How is PHP supposed to know that the function works the same? – Ry- Jul 30 '12 at 01:20
  • what is PDO? sorry im a newbie – user1349313 Jul 30 '12 at 01:23
  • @user1349313: It's a better way of accessing databases in PHP. Reading material: http://php.net/manual/en/book.pdo.php http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ – Ry- Jul 30 '12 at 01:24
1

Unless I specifically need to include a file more than once (say a template of something that is in more than one place on the page) I always use require_once. Though in the case of a template, making that template a class which can have multiple instantiations would be a better pattern, and thus that file would only get included in once. For example:

class TemplateExample() {
    function display() {
        echo "I'm a template!";
    }
}

and

$template = new TemplateExample();
$template->display();
echo "Some text between the templates.";
$template->display();

It's also worth noting that there are some sources out there that say not to use require_once or include_once because they are slower than require and include. While it is true that they are slower, it is not noticable; it would take something on the order of 10,000 includes before your script was slowed by a millisecond. This difference is so small that you should not worry about the speed.

Michael Fenwick
  • 2,374
  • 2
  • 19
  • 28