Re: include_once() and require_once() in function



Andrew G. Koptyaev wrote:
Is I can use include_once() or require_once() in function or only include()?



If you use include_once the file will only be included the first time the function is called, if you use include it will be included each time the function is called. (require and require_once follow the same principle obviously.)

You need to consider what is in the file you are including, and what scope it should have. For example:

If the file you include sets some variables and you use include_once, the second time you call the function the file will not be included again, though the variables won't be already defined because their scope was only within the function.

However if the file you include defines a function and you use include, the second time you call the function you will get an error trying to redefine the function since it has global scope.
.



Relevant Pages

  • Re: Global Variables...
    ... it comes down to readabilty and maintainability ... ... on the scope of such definitions. ... Andrew ... > Do you put them in a unit or a code module? ...
    (borland.public.delphi.language.objectpascal)
  • Re: Newbie Question
    ... > When I executed your code I received an exception in the main thread. ... If you moved the scope of 'i' ... outside the if (_int_ i=0;..) bit it should ... (also as Andrew H. pointed out). ...
    (comp.lang.java.help)
  • Re: $valPHPPath already declared error when I use this block
    ... second time; it doesn't exist obviously within the scope of the proc ... since [info exists valPHPPath] is false. ...
    (comp.lang.tcl)
  • Re: include_once() and require_once() in function
    ... Jamie Furness wrote: ... If the file you include sets some variables and you use include_once, the second time you call the function the file will not be included again, though the variables won't be already defined because their scope was only within the function. ... I use the _once form to avoid things like double declarations. ...
    (comp.lang.php)
  • Re: Best budget computerized telescope is?
    ... You might want to try get getting your scope repaired under the guarentee. ... >> the ETX Premier Series and dealers may be ... > second time in a year - I am less than convinced. ...
    (sci.astro.amateur)