Jump to content


Photo

Include Vs Require


  • Please log in to reply
8 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 30 August 2006 - 01:47 PM

Are they the same or is there a difference?

If there is a difference, can somebody please tell me what that would be...

...Thanks in advance ;D

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 30 August 2006 - 01:52 PM

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.

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 30 August 2006 - 01:53 PM

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.
follow me on twitter @PHPsycho

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 30 August 2006 - 01:54 PM

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.

Incorrect.

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 30 August 2006 - 01:55 PM

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 ;)
follow me on twitter @PHPsycho

#6 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 30 August 2006 - 01:55 PM

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
I don't need your script, I'll try to write it myself

#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 August 2006 - 01:56 PM

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.


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 ;-)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 30 August 2006 - 01:57 PM

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 ;)

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.

#9 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 30 August 2006 - 01:58 PM

LOL

Sorry I have ben around for too long!!!

I learnt that many moons ago - and its stuck. But hey you learn new stuff.
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users