Mohammed A. Al Jama
Mohammed A. Al Jama

Reputation: 511

Are there any downsides from using Include() or nested include's()?

I am not an expert in PHP. I am trying to increase the use of include() to make my website code as clean as possible instead of just copying, for example, code of the header in all the pages. I have two questions

1 . Is it good practice to use include() a lot in terms of server requests, speed, ...etc ?

index.php

(bunch of code)

  <? include("connect.php") ?>;

(bunch of code)

 <? include("header.php") ?>;

(bunch of code)

 <? include("footer.php") ?>;

2 . Is it fine also to use nested include's()? example:

header.php (some code)

<? include("searchFormInput.php") ?>;

now index.php will include header.php, then header.php will include searchFormInput.php as well is this fine?

Thanks a lot

Upvotes: 0

Views: 257

Answers (5)

vgardner
vgardner

Reputation: 517

It's fine to that in terms of performance and it keeps your code clean and reusable to a certain extent. However, I recommend that you use templates for this kind of code inclusion, where you load all your information into variables and then call them in your template. Consider a template engine http://www.smarty.net/ or maybe a PHP framework or CMS http://drupal.org/ which should make your life easier in the short and long run!

Upvotes: 1

Tadeck
Tadeck

Reputation: 137360

Yes, including is a common practice.

Yes, including gives you slight performance penalty (very small).

But including gives you readibility gain and thanks to it will be easier to employ DRY rule. Just remember the following:

  • if the file contains some code that should be executed only once (some setup, class definitions, function definitions etc.), use include_once() (it will have no effect if invoked again on the same filename),

  • if the file contains some code executed multiple times (eg. some template for a form), use simple include(),

  • if something is required for your application to work (eg. some security code, some setup etc.), use require() instead of include() or require_once() instead of include_once() - if the file will not be found, PHP will throw fatal error and will stop executing your script,

Upvotes: 2

Shakti Singh
Shakti Singh

Reputation: 86406

I think you should look at autoload with PHP once. You should not need to care the include of every and each file. Just adjust your autoload and that will take care of all these. You needs to do object oriented programming with this.

Upvotes: 1

ahm_selim
ahm_selim

Reputation: 141

include,require will read file and excute codes inside it.

i made some tests on speed of include and file_get_contents results was include and require is slow in compare

so my advice don't increase numbers of inclusion.

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270637

The principle downside to nesting includes is that you are likely to run into situations when cross-dependencies cause a file to be include more than once. That is easily solved by the use of include_once(), though.

In your example however, with header.php including searchFormInput.php, you probably won't have problems assuming these files both mostly produce HTML output rather than parsing classes and dependencies.

On the other hand, if you had some structure like

connect.php includes config.php
session.php includes config.php

you would need to use include_once('config.php').

Upvotes: 2

Related Questions