Include and include_once in PHP

Could someone explain to me when to use include and include_once and how to use it.

I am new to PHP and would like to understand that these are laymans terms. It's not entirely clear what was mentioned in the PHP documentation at www.php.net.

Let's say I have the following folder structure

-->Root-->API-->User.php -->Root-->Users-->CreateUser.php 

If I need to use User.php in CreateUser.php, how should I do it.

And if in

there is another file
 -->Root-->API-->Utility-->ImageProcessor.php 

How should I include ImageProcessor.php with CreateUser.php

+4
source share
5 answers

If you always include_once , everything will be fine. It prevents the inclusion of the same file twice.

Let's say you have these files:

c : include b; include a; include b; include a;

b : include a;

a : echo "hello";

when you execute c , a will be turned on twice (most likely, this is an undesirable situation), so if you use all of them as include_once , it will be called only once.

It comes with a small cost, however, if you are not Facebook or so, it is not a significant cost.

+11
source

In CreateUser.php:

 include_once("/API/User.php"); include_once("/API/Utility/ImageProcessor.php"); 

If you need to make sure that these files are included, you should use require_once instead ...

+4
source

Use include_once if it implies an implicit nested requirement (any better term?). I mean something like this:

Suppose

  • A is a department model
  • B - employee model
  • C - database module

Since the model requires a database connection, A and B include C. The department consists of employees working there, so A includes B. With include_once, the database module will be turned on only once, so there will be no duplicate declaration error.

Include is something more general, such as an output template (possibly a duplicate of the active username) that is intentionally created several times.

+4
source

I will use simple examples, one for when include () is better, one for include_once () is better.

Say you have a.php and b.php files. Each of them includes "library.php", which contains the function foo (). Now, if you try to "include" both a.php and b.php in another file, for example index.php, you may receive the error message foo (). Which means include_once () is better suited for this situation. You cannot define the same function twice.

The second case. Suppose you want to "include" file.php every time your loop runs. The contents of file.php may be a 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 starts. Include_once () will only work for the first time.

+3
source

The include_once () statement includes and evaluates the specified file during script execution. This behavior is similar to the include () statement, with the only difference being that if the code from the file is already included, it will no longer be included. As the name implies, it will be enabled only once. For example, I have three files,

FILES:

  • functions.php
  • GLOBALS.PHP
  • header.php

Here's what 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 receive an error message because GLOBALS.PHP already contains FUNCTIONS.PHP. You will receive a message stating that the function foo () has already been declared in GLOBALS.PHP and is also included in HEADER.PHP, which means that I turned on FUNCTIONS.PHP twice. Therefore, to enable only FUNCTIONS.PHP only once, I have to 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 no longer get the error, because PHP knows that including the FUNCTIONS.PHP file is ONCE only. Therefore, to avoid getting the error, it would be safe to use include_once () instead of the include () function in your PHP code.

+1
source

All Articles