The major difference between include () vs include_once() in php is,
The both functions working performance is same
The include_once() is used to include any user defined which was written by you functions.php script. As the name suggests, it will be included just once.
In details now understand by below example
Let's i have 3 files
FUNCTIONS.PHP
GLOBALS.PHP
HEADER.PHP
FUNCTIONS.PHP
<?php
function funname(){
echo "Hello World..!";
}
?>
GLOBALS.PHP
<?php
include "FUNCTIONS.PHP";
funname();
?>
HEADER.PHP
<?php
include "FUNCTIONS.PHP";
include "GLOBALS.PHP";
funname();
?>
If you try to access the HEADER.PHP you will get an error, this is about you already declared function in GLOBALS.PHP, and again included in HEADER.PHP - which means you included same FUNCTIONS.PHP by 2 times.
So the best practice rewrite the code as below
HEADER.PHP
<?php
include_once ('FUNCTIONS.PHP');
include "GLOBALS.PHP';
?>
now if you try to access the HEADER.PHP, you will never get error.
Note: In short remember when we have functions in the script at this time better to use include_once() else you can use include()
I hope you understood the difference
The both functions working performance is same
The include_once() is used to include any user defined which was written by you functions.php script. As the name suggests, it will be included just once.
In details now understand by below example
Let's i have 3 files
FUNCTIONS.PHP
GLOBALS.PHP
HEADER.PHP
FUNCTIONS.PHP
<?php
function funname(){
echo "Hello World..!";
}
?>
GLOBALS.PHP
<?php
include "FUNCTIONS.PHP";
funname();
?>
HEADER.PHP
<?php
include "FUNCTIONS.PHP";
include "GLOBALS.PHP";
funname();
?>
If you try to access the HEADER.PHP you will get an error, this is about you already declared function in GLOBALS.PHP, and again included in HEADER.PHP - which means you included same FUNCTIONS.PHP by 2 times.
So the best practice rewrite the code as below
HEADER.PHP
<?php
include_once ('FUNCTIONS.PHP');
include "GLOBALS.PHP';
?>
now if you try to access the HEADER.PHP, you will never get error.
Note: In short remember when we have functions in the script at this time better to use include_once() else you can use include()
I hope you understood the difference
0 comments:
Post a Comment