Jump to content

Include Vs Require


master82

Recommended Posts

From the manual:

require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
Link to comment
Share on other sites

include will include your file whenever the flow of code calls it.

require will include your file regardless...

eg.

if ($string == 'yep')
{
include('file.php);
}

will only include your file if $string is 'yep'.

if ($string == 'yep')
{
require('file.php);
}

will include it no matter what $string is.
Link to comment
Share on other sites

[quote author=ToonMariner link=topic=106246.msg424672#msg424672 date=1156945987]
include will include your file whenever the flow of code calls it.

require will include your file regardless...

eg.

if ($string == 'yep')
{
include('file.php);
}

will only include your file if $string is 'yep'.

if ($string == 'yep')
{
require('file.php);
}

will include it no matter what $string is.
[/quote]Incorrect.
Link to comment
Share on other sites

from the manual

Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.

apology accepted ;)
Link to comment
Share on other sites

well, yes there is a difference:
1. when using include, script continuous running anyway, for example: you have a header included, and this header.php files has been deleted, script will display averything but header...
2. when using require script will stop running if there is any error, in my example it won't display enything but an error
Link to comment
Share on other sites

[quote author=ToonMariner link=topic=106246.msg424672#msg424672 date=1156945987]
include will include your file whenever the flow of code calls it.

require will include your file regardless...

eg.

if ($string == 'yep')
{
include('file.php);
}

will only include your file if $string is 'yep'.

if ($string == 'yep')
{
require('file.php);
}

will include it no matter what $string is.
[/quote]

this is not accurate at all. go back and read GingerRobot's response. she has the right of it, and imagine that: she actually got the answer from the manual! why can't more people do that... lol ;-)
Link to comment
Share on other sites

[quote author=ToonMariner link=topic=106246.msg424675#msg424675 date=1156946138]
from the manual

Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.

apology accepted ;)
[/quote]No apology given. Take note of "prior to 4.0.2"

Any host still running < 4.0.2 is a poor host ;) Your apology accepted.

Also note that the code will not be executed even in < 4.0.2. require() just tries to read the file. Another apology accepted.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.