Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include and include_once in PHP

Tags:

include

php

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 CreateUser.php

like image 869
Abishek Avatar asked Jun 16 '11 15:06

Abishek


4 Answers

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.

like image 98
ahmet alp balkan Avatar answered Sep 18 '22 12:09

ahmet alp balkan


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.

like image 40
afaf12 Avatar answered Sep 21 '22 12:09

afaf12


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.

like image 30
LeleDumbo Avatar answered Sep 19 '22 12:09

LeleDumbo


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.

like image 39
AllisonC Avatar answered Sep 22 '22 12:09

AllisonC