By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

include_once() and require_once() in function

P: n/a
Is I can use include_once() or require_once() in function or only include()?
Sep 29 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

Hi Andrew,

You can use all 3 mentioned.
Why don't you simply try and see?

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 29 '08 #2

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

I tend to use require_once for most things.

The 'require' part causes your script to stop executing with a fatal
error if it can't include your file, which is generally what you would want.

The 'once' part means if a script has already been included further back
in time it won't be included again (as this could cause all manner of
problems).
Roger Heathcote.
Sep 29 '08 #3

P: n/a
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.
Sep 29 '08 #4

P: n/a
Jamie Furness wrote:
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.
Generally, the only times I use require or include as opposed to
require_once or include_once, is when I specifically want that include
to appear more than once in the script. For example, I might have files
with month, day, and year. If, on my form I have start date and end
date, then I would be using each of these twice.

Otherwise, I use the _once form to avoid things like double declarations.
Oct 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.