Thursday, December 25, 2014

How to Include files in PHP ?

A PHP file can also be included in other PHP file using file include method. There are two PHP functions which can be used for including one PHP file into another PHP file.
The include() Function /[include_once()]
The require() Function/ [require_once()]

Note:Including file has two functions include() and require() ,however include_once and require_once are also practiced as per requirements.

Syntax:
 include 'filename.php';  
 include_once'filename.php';  
 require'filename.php';  
 require_once'filename.php';  

Example:

PHP Script (demo.php)

 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta charset="UTF-8">  
 <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <?php  
 echo "<h3>It comes from included file</h3>";  
 ?>  
 </body>  
 </html>  


PHP Script (include.php)
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta charset="UTF-8">  
 <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <?php  
 include "demo.php";  
 echo "See what is includeed";  
 ?>  
 </body>  
 </html> 




No comments:

Post a Comment