13 Replies - 3824 Views - Last Post: 27 September 2011 - 12:04 PM

Poll: include or require? (9 member(s) have cast votes)

Which do you prefer, include, ore require?

  1. Include (5 votes [55.56%] - View)

    Percentage of vote: 55.56%

  2. Require (3 votes [33.33%] - View)

    Percentage of vote: 33.33%

  3. Doesn't matter (1 votes [11.11%] - View)

    Percentage of vote: 11.11%

Vote Guests cannot vote

#1 Creecher  Icon User is offline

  • I don't care
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,049
  • Joined: 06-March 10

Include, or Require?

Posted 12 February 2011 - 09:49 AM

I've been wondering, which function does the D.I.C community prefer, include();, or require();?


I know that if an error occurs with include, it doesn't stop the script from running and keeps going, but if you get an error with require, it's fatal, and makes the script stop. When I write PHP I usually use includes. But I think it comes down to personal preference than anything.


So which is it, D.I.C heads?

Is This A Good Question/Topic? 0
  • +

Replies To: Include, or Require?

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Include, or Require?

Posted 12 February 2011 - 09:56 AM

I personally use both. My usage depends on whether the included script is necessary for the page to be useful, or if it's just some of the "icing on the cake", so to speak. For instance, if I had a Twitter feed I loaded via an include/require, I'd use include. But for something like the database connection, gotta be require.
Was This Post Helpful? 2
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,324
  • Joined: 08-June 10

Re: Include, or Require?

Posted 12 February 2011 - 10:09 AM

for anything that is essential (function/class/constants definitions) I use require. things that don’t break the script (HTML includes) get an include. if the HTML include is required, I rather use file_get_contents().

PS. include & require are not functions, thus you should call them without the parentheses.
Was This Post Helpful? 2
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Include, or Require?

Posted 12 February 2011 - 10:13 AM

View PostDormilich, on 12 February 2011 - 12:09 PM, said:

PS. include & require are not functions, thus you should call them without the parentheses.


Actually, according to PHP.net, they are. They can be called three things, technically: functions, statements, and control structures, all of which are correct.

This post has been edited by Valek: 12 February 2011 - 10:14 AM

Was This Post Helpful? 1
  • +
  • -

#5 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Include, or Require?

Posted 12 February 2011 - 11:11 AM

*
POPULAR

View PostNeverPool, on 12 February 2011 - 11:49 AM, said:

But I think it comes down to personal preference than anything.

No, it does not come down to personal preference - the semantics of the two are different. You should use require for required dependencies - things like data access classes, where failure to load the file pretty much dooms the rest of the script. Use include for cases where failure to load the file is acceptable. If you want to use include for the former case, then you need to write additional code check whether the load was successful and handle failure gracefully. Of course, that's usually pointless, because in such cases there's not much you can do other than display an error message, which is why we have require in the first place.

View PostValek, on 12 February 2011 - 12:13 PM, said:

Actually, according to PHP.net, they are.

No, Dormilich is correct. Both include and require are technically language constructs, like echo and print, not functions. Though they may be superficially similar, they aren't actually the same thing and don't behave the same way. If you don't believe me, try using them like a "normal" function. For example:
$func = 'include';
var_dump(function_exists($func));
$func('/path/to/some/file.txt');


If $func was, say, "readfile", then the function_exists($func) would return true and the last line would dump the target file to the output stream. However, with "include" or "require", the function_exists($func) returns false and the last line generates a fatal error.
Was This Post Helpful? 5
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Include, or Require?

Posted 14 February 2011 - 06:47 PM

My previous post was rather dumb, mostly attributable to a week without sleep. You're absolutely correct.

I'd also like to clarify some other issues with my previous post, so others here don't get the wrong idea about those two constructs. Neither one of them is really a control structure itself. They only handle flow control as far as the script they are including does. They themselves are not directly responsible for it aside from require killing the script on error. Calling them statements is arguably acceptable, though, but construct is actually the proper term for them.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,324
  • Joined: 08-June 10

Re: Include, or Require?

Posted 14 February 2011 - 11:57 PM

yepp, the Manual calls them language constructs.
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Include, or Require?

Posted 15 February 2011 - 12:10 AM

The manual actually calls them statements and constructs directly, and functions and control structures indirectly (likely for ease of locating them, and so they'll show up in function searching for newbs). In the end, all that really does is confuse those newbies though, especially if they care to actually progress in learning the difference between each of those elements.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,324
  • Joined: 08-June 10

Re: Include, or Require?

Posted 15 February 2011 - 12:27 AM

View PostValek, on 15 February 2011 - 08:10 AM, said:

In the end, all that really does is confuse those newbies though, especially if they care to actually progress in learning the difference between each of those elements.

that amount should be fairly small (judging from my experience).
Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Include, or Require?

Posted 15 February 2011 - 12:51 AM

My experience agrees with that, as well.
Was This Post Helpful? 0
  • +
  • -

#11 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Include, or Require?

Posted 15 February 2011 - 07:38 PM

both is the best way to go. like for instance you want to use require if it's a php file that's going to connect you to a database and the rest of the page needs data from that database and basically include for pictures, inc files etc...
Was This Post Helpful? 0
  • +
  • -

#12 carlosm7  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 22-April 10

Re: Include, or Require?

Posted 21 February 2011 - 01:56 PM

I don't know about you, but I don't want PHP taking care of any error reporting to my users!

I have a very old PHP book (Core PHP Programming, Third edition, Prentice Hall) that says there is other difference, and it's that include will be executed every time PHP runs across it, while require will be actually replaced by the file you are trying to require.

For example, if you "include" a file in a loop that runs several times, and each time you change the name of the file, then each one of the files will be included in the appropriate loop iteration, but if you require them, then the first required file will be included on all of the loop iterations, because the require statement will have been replaced by the first required file.

May be the book is/was wrong, or at least once upon a time require worked like that, just not anymore. For this reason I will vote for include.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,324
  • Joined: 08-June 10

Re: Include, or Require?

Posted 21 February 2011 - 03:58 PM

View Postcarlosm7, on 21 February 2011 - 09:56 PM, said:

I don't know about you, but I don't want PHP taking care of any error reporting to my users!

with some sensible error handling on your side, that shouldn’t be a problem.

View Postcarlosm7, on 21 February 2011 - 09:56 PM, said:

I have a very old PHP book (Core PHP Programming, Third edition, Prentice Hall) that says there is other difference, and it's that include will be executed every time PHP runs across it, while require will be actually replaced by the file you are trying to require.


maybe in old versions, but in PHP 4/5 they work the same way:

the PHP manual said:

require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

Was This Post Helpful? 0
  • +
  • -

#14 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Include, or Require?

Posted 27 September 2011 - 12:04 PM

Don't really have much to say that hasn't been said, except that I use include, include_once and require_once.

My reasons being:

include when I'm including a non-essential file which I may use more than once (e.g, sub-templates)

include_once when I know I'll only need a non-essential file once and
require_once for essential files, both for reasons of working with others, or my own forgetfulness, no need to impede that server queue with multiple requests :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1