Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Difference Between the include() and include_once() Functions

 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.

ok, what does this mean exactly? 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.
if you know you are messy with your PHP code, then its safer to use the include_once(), but if you keep track of all your code, then its ok to use the include() function.

hope i helped you explained the differences between the include() and the include_once() functions in php

If you have any query or suggestions, feel free to ask me via the comment section below.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments