p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Include and require files...


Message #1 by Roy Van Tassel <jazzsnob@o...> on Fri, 13 Sep 2002 03:11:09 -0400
hello. i was wondering if you could place an include file within another
include? I have a page with the layout of a site. the navigation is within a
table, that is in a nav include file, and the code that surrounds it, plus
the include, are in another include file. is there any way to do this? i
can't find any documentation about it, but i was told you could do this...

example:
includeFile1:
<table>etc....<? include "includeFile2.inc" ?>etc...</table>

thanks in advance...


Message #2 by "Nikolai Devereaux" <yomama@u...> on Fri, 13 Sep 2002 08:39:36 -0700
Yes, you can include or require how ever many files you want, from any
files.

The only caveat is that you have to beware of recursion -- for example:

 -- file1.php
<?php
 include('file2.php');

 ...
?>

-- file2.php
<?php
  include('file1.php');

  ...
?>


I usually get around this by only including files that provide function or
class definitions, but don't in themselves execute any code.  (Aside from
setting configuration variables).

Then, when I need the file, I use require_once() instead of require(), which
prevents a file from being parsed and executed multiple times.


hth,

nik

Message #3 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Fri, 13 Sep 2002 10:19:27 -0700
> Then, when I need the file, I use require_once() instead of
> require(), which
> prevents a file from being parsed and executed multiple times.

Two questions then:

1. Is there ever a good reason to use require() instead of 
require_once()?

2. Is there ever a good reason to use include()?

On (2.), the docs reads:

"include() differs from require() in that the include statement is 
re-evaluated each time it is encountered (and only when it is being 
executed), whereas the require() statement is replaced by the required 
file when it is first encountered, whether the contents of the file will 
be evaluated or not..."

I could see this being used to load a subset of one's function 
libraries, keeping the total amount of loaded code to a minimum. 
However, I could also see this being more hassle than it's worth. Beyond 
the logic required to make it happen, there would be extra reads, 
re-parsing required of the PHP engine, etc.

dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************

  Return to Index