Could someone please explain to me on when should one use the include
and include_once
and how should one be using it.
I am new to PHP and would like to understand this is laymans terms. Not too clear with whats mentioned on PHP documentation at php.net.
Let us say I have the following folder structure
-->Root-->API-->User.php
-->Root-->Users-->CreateUser.php
If I have to use the User.php
in CreateUser.php
how should I be doing it.
And if there is another file under
-->Root-->API-->Utility-->ImageProcessor.php
How should i being including ImageProcessor.php
with CreateUse开发者_开发百科r.php
If you always include_once
everything will be ok. It prevents including the same file twice.
Let's say you have these files:
c
: include b; include a;
b
: include a;
a
: echo "hello";
when you execute c
, a
will be included twice (most probably an unwanted situation), therefore if you use all of them as include_once
it will be called only once.
It comes with a little performance cost however if you are not Facebook or so, that is not a significant cost.
I'll use simple examples, one for situation when include()
is better, one when include_once()
is better.
Let's say you have files a.php
and b.php
. Each of them "includes" library.php
which contains function foo()
.
Now, if you try to include
both a.php
and b.php
in another file, index.php
, for example, you could get an error saying that foo()
was already declared. Which means include_once()
is better suited in this situation. You'll avoid defining the same function twice.
Second case. Let's assume you want to include
file.php
every time your loop runs.
Content of file.php
could be just simple HTML output.
file.php
:
<?php echo "User No: " . $i; ?>
index.php
:
<?
for($i=1; $i<=10; $i++){
include("file.php");
}
In this case, include()
is better because it will include the file every time the loop runs. Include_once()
would only work the first time.
Use include_once
when there's a implicitly nested requirement (any better term?) involved. I mean something like this:
Assume
- A is a department model
- B is a employee model
- C is a database module
Because model needs database connection, A and B both includes
C. A department consists of employees working there, so A includes
B. With include_once
, the database module would only be included just once, so there won't be any duplicate declaration error.
Include
is for something more general like a template of output (perhaps echoing active username) that is intentionally designed to appear multiple times.
In CreateUser.php
:
<pre><code>
include_once("/API/User.php");
include_once("/API/Utility/ImageProcessor.php");
</pre></code>
If it is necessary to make sure these files are included, you should use require_once
instead.
The include_once()
statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include()
statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once. Lets say for example, i have three files,
FILES:
FUNCTIONS.PHP
GLOBALS.PHP
HEADER.PHP
This is how each file looks like:
FUNCTIONS.PHP
:
<?php
function foo(){
echo 'some code';
}
?>
GLOBALS.PHP
:
<?php
include('FUNCTIONS.PHP');
foo();
?>
HEADER.PHP
:
<?php
include('FUNCTIONS.PHP');
include('GLOBALS.PHP');
foo();
?>
Now if you try to open HEADER.PHP
you will get an error because GLOBALS.PHP
includes FUNCTIONS.PHP
already. You will get an error saying that function foo()
was already declared in GLOBALS.PHP
, and I also included in HEADER.PHP
- which means I have included FUNCTIONS.PHP
two times.
So to be sure I only include FUNCTIONS.PHP
only ONE time, I should use the include_once()
function
, so my HEADER.PHP
should look like this:
HEADER.PHP
:
<?php
include_once('FUNCTIONS.PHP');
include('GLOBALS.PHP');
?>
Now when I open HEADER.PHP
, I will not get an error anymore because PHP knows to include
the file FUNCTIONS.PHP
only ONCE
So to avoid getting an error, it would just be safe to use include_once()
instead of include()
function in your PHP code.
精彩评论