PHP include and include_once -Functions for File Inclusion

File inclusion is important feature in any coding language to apply modularity and code reusability in software applications. File inclusion means that if you have coded certain operations in one of your programming projects, you can use the same tested and verified codes in other applications in future. PHP include and include_once statements can be used in Web applications to add PHP code from other scripts.  

File inclusion can also be used to standardize the functionality of your web application. Like creating templates, using same functions in different sections of an application and uniform functionality. You can create a library of functions for validation or database connection for your web application and use it all scripts.

Where to add PHP include or include_once?

PHP include functions can be added in an HTML file or a PHP file. It must be added before you use the functions, variables or constants defined in the included script file. If you are including file  in a web page then include function must be enclosed in php tags for scripting

If you want to include a predefined scrippt file in a PHP file then it must be added with a function call to include or include_once preferably at the beginning of your PHP file. Though, you can call this function anywhere in your file to include another script file.

Syntax

include(‘/path/file’)

include_once(‘/path/file’)

What these functions do?

Every time your code includes a call to include or include_once function it looks for the file passed as parameter under the path mentioned.  If the file is located, it is evaluated and the specific code of the file is executed whenever some function from the included file is called or some variable is used.

 If the file parameter in include function is not present at this location, it will be searched in the current working directory and the directory where the calling script file is saved. If your application fails to locate the file, a warning is generated.

The variables declared in the included files inherit the scope of the script where PHP include or include_once  function is called. All the declared functions and classes within the included file are global in scope.

Return value of include functions

If the file is successfully included in the script, include functions return TRUE (1). If the file inclusion is unsuccessful, include functions return FALSE(0).

PHP  include

The file is evaluated every time a call to include function is executed.  It means that the functions in this file will be redefined with every include function call. If the script file has already been included  and evaluated, it will raise a warning. Rest of the statements after second include statement to same script file will not be executed.

PHP include_once

The included script file is evaluated only once in the specific script file where the include_once function is executed.  It means that the functions or variable of the included file will be not be redefined again. No warning message will be displayed. The remaining script statements will be executed. If other include_once function are encountered, they will be ignored.

PHP  include and include_once Example

In this example we have created a script file ‘include.php’ with one function netpay() that calculates and returns netpay for basic salary passed as an argument to it.

<?php
function netpay($sal)
{
	return $sal+$sal*.12+$sal*.40-$sal*.09;
}	
?>

Using include

<?php 
	 include('include.php');	
?>
<h2> Salary:<?php echo netpay(4000);?></h2>
<?php 
	 include('include.php');	
?>
<h2> Salary:<?php echo netpay(6000);?></h2>
<?php 
	 include('include.php');	
?>
<h2> Salary:<?php echo netpay(8000);?></h2>
PHP include

Using include_once

<?php 
	 include_once('include.php');	
?>
<h2> Salary:<?php echo netpay(4000);?></h2>
<?php 
	 include_once('include.php');	
?>
<h2> Salary:<?php echo netpay(6000);?></h2>
<?php 
	 include_once('include.php');	
?>
<h2> Salary:<?php echo netpay(8000);?></h2>
PHP include_once

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *