p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: difference require( ) and include( )


Message #1 by "Jimmy Bellmon" <webmaster@v...> on Tue, 17 Sep 2002 00:36:14 -0400
What's the difference between the require( ) and include function and when
should each be used. I just bought the book "PHP and MySQL Web Development"
from SAMS, and the book is really good. Though the book explains it, I would
just like someone who is used to using these to explain when using one of
the other would be best. From what I have read about them I feel I will be
using them a lot, I just want to have an upfront user response.
Thanks



Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 17 Sep 2002 09:09:46 -0700
The difference is simple --

include() will throw a warning, but continue processing the script, if the
include file is not found, whereas require() will throw an error and exit
the script if the include file is not found.


If you have a file that simple provides function or class definitions, but
no executable code, then you only need the file to be parsed once for all
the contents to be in the PHP interpreter's symbol tables.  For these files,
I suggest using  require_once()  or  include_once().


If, on the other hand (perhaps I should've said 'else'!), you have a file
which actually executes some code, and you need to have that code executed
several times, use include()  or  require().

Each time include() or require() are called, the contents file to be
included are basically inserted in-place just after the include/require
call.  This means that if you have these two files:

file1.php
<?php
  echo $count . "\n";
?>


file2.php
<?php

for($count = 0; $count < 5; ++$count)
{
  include('file1.php');
}
?>

You'll get the following output:

0
1
2
3
4


If you're interested in how I do things, I rarely, if ever, use include() or
require().  99% of the time, if not more, I use require_once().

hope this clears things up a bit!


Nik

Message #3 by spam@k... on Fri, 20 Sep 2002 14:02:18 -0500
In my simple content management system, at the top of the control panel, I have this block of includes: 

include("mcConfig.php");
include("mcBaseFunctions.php");
include("mcUserSystem.php");
include("mcDatabaseClass.php");
include("mcNavFunctions.php");
include("mcArticleFunctions.php");
include("mcCatPageFunctions.php");
include("mcEmailFunctions.php");
include("mcQuoteFunctions.php");
include("mcStyleSheetFunctions.php");
include("mcUploadDownloadFunctions.php");
include("mcTemplateFunctions.php");
include("mcCommentsFunctions.php");
include("mcOtherSitesFunctions.php");
include("mcInstallScript.php");

I was thinking of redoing this so that each file would only be included if needed. The first 4 need to always be included, but the
others don't need to be included every time the page loads, and the presense or absence of a single variable usually tells me what
page needs to be loaded. Do you think this would save much time loading the page, or do you think it is a useless gesture? If things
are included conditionally, is require_once() still your preference? 





------------------------------------------------
On Tue, 17 Sep 2002 09:09:46 -0700, "Nikolai Devereaux" <yomama@u...> wrote:

> 
> The difference is simple --
> 
> include() will throw a warning, but continue processing the script, if the
> include file is not found, whereas require() will throw an error and exit
> the script if the include file is not found.
> 
> 
> If you have a file that simple provides function or class definitions, but
> no executable code, then you only need the file to be parsed once for all
> the contents to be in the PHP interpreter's symbol tables.  For these files,
> I suggest using  require_once()  or  include_once().
> 
> 
> If, on the other hand (perhaps I should've said 'else'!), you have a file
> which actually executes some code, and you need to have that code executed
> several times, use include()  or  require().
> 
> Each time include() or require() are called, the contents file to be
> included are basically inserted in-place just after the include/require
> call.  This means that if you have these two files:
> 
> file1.php
> <?php
>   echo $count . "\n";
> ?>
> 
> 
> file2.php
> <?php
> 
> for($count = 0; $count < 5; ++$count)
> {
>   include('file1.php');
> }
> ?>
> 
> You'll get the following output:
> 
> 0
> 1
> 2
> 3
> 4
> 
> 
> If you're interested in how I do things, I rarely, if ever, use include() or
> require().  99% of the time, if not more, I use require_once().
> 
> hope this clears things up a bit!
> 
> 
> Nik
> 
> 
> 
Message #4 by CathyV227@a... on Fri, 20 Sep 2002 23:42:00 EDT
???????

Message #5 by "Nikolai Devereaux" <yomama@u...> on Mon, 23 Sep 2002 11:41:48 -0700
> I was thinking of redoing this so that each file would only be
> included if needed. The first 4 need to always be included, but the
> others don't need to be included every time the page loads, and the
> presense or absence of a single variable usually tells me what page
> needs to be loaded. Do you think this would save much time loading
> the page, or do you think it is a useless gesture? If things are
> included conditionally, is require_once() still your preference?

Put it this way -- I don't include files unless they're required.  In that
sense, I don't have any files that are "included conditionally", in the context
that you suggest.

Again, the only difference between include and require is that include will NOT
terminate your script if the file to be included is not found.


It doesn't make much sense to include files that are not used by the page
that's including them.


hth,

nik


  Return to Index