Forum Moderators: coopster

Message Too Old, No Replies

using includes

         

sssweb

9:56 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Does it cause an error to 'include' a file more than once in a php page?

I have a page that uses 'include' statements to call a script for email handling. The two includes are in separate 'if' conditionals, one right after the other.

It works fine if only one of the includes fires, but if both are called, the page stops at the second include.

Am I better off just calling it once, at the very start of the page (before all conditionals), or does it need to be called at the line it's used? If the latter, how do I handle this error?

Sekka

9:59 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



include_once() is your man. It checks the file, and if it has already been included, it will be ignored.

sssweb

10:29 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Thanks -- Use that in each instance, or only AFTER the first one; i.e.:

include(emailer.php)...

include_once(emailer.php)...

include_once(emailer.php)...

eeek

4:00 am on Oct 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use it (or require_once) every time.

Sekka

9:20 am on Oct 29, 2008 (gmt 0)

10+ Year Member



As eeek said, use it all the time, so you know that it will never be included more than once and give you an error.

include_once() and require_once() behave exactly the same as include() and require() except they check the file they are including and ignore it if it has already been included.

Couple of things to note though,

* require() and require_once() throw fatal errors if the include fails, where as include() and include_once() only through a warning and the script execution continues.

* the *_once() commands can be slower because they have to check the files, but it is barely noticeable

sssweb

1:44 pm on Oct 29, 2008 (gmt 0)

10+ Year Member



Thanks guys.