PHP include vs require_once

After 10 years of PHP programming, it has finally settled with me the difference between require_once() and include().

Now, there are other differences as recorded in the manual, but I never really cared about it, until today.

I have 4 files: index.php, config.php, functions.php, and plugins/config.php.  All of which are nested inside the other respectively using an include.

In the last config.php file, I am declaring a function…say, function what(){ }

While using include, I get the error, cannot redeclare function what as previously declared.  The interesting thing was that the two places it said that it was begin redeclared in were the same file!

Strange.  I happened to realized that during the order in which PHP includes files, was that it was redeclaring the function as it bubbled up the chain of includes.

changing to require_once() fixed the problem!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: