Home

Monday, April 4, 2011

Difference between include() and include_once() in php

function.php
<?php
function foo(){
echo 'some code';
}
?>
Global.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 global.php includes function.php already. you will get an error saying that function foo() was already declared in global.php, and i also included in Header.php - which means i have included function.php two times.

so to be sure i only include function.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 function.php only ONCE  

 

No comments:

Post a Comment

rathoddhirendra.blogspot.com-Google pagerank and Worth