Posted By:
7 Mar, 2014 11:02 pm PST

How to include content of external PHP file current PHP file

 1)               How to include content of external PHP file current PHP file?

 

If you working for large PHP application then work is divided among different people and among each individual work will be divided into different modules and different functionalities. So it is not always feasible to write entire code on single file. The entire work divided into number of different files. A great feature provided by PHP is the ability to load a script from an external file; this makes it much easier to organize your code in larger projects.

 

PHP provides four constructs you can use to load an external script:

Include, include_once,require, and require_once.

 

Syntax:-

include ‘fileName’;

or

include_once ‘fileName’;

 

required ‘fileName’;

or

required_once ‘fileName’;

     
 

<?PHP

 

echo "<h2 >";

 

echo "To calculate square and cube of number <br ><br >";

 

echo " Welcome to { $_SERVER [' SERVER_NAME ']} <br ><br >";

 

function square ( $no)

 

{

 

return ( $no * $no );

 

}

 

function cube ( $no)

 

{

 

return ( $no * $no * $no );

 

}

 

?>

MainFile.php

 
 
   

<?php

include 'MainFile .php ';

// this will include specified page to-this script at this position

$num = rand (1 ,100) ;

 

$sqr = square ( $num ); // calling methos return in MainFile .php

echo " The Square of { $num } is { $sqr }<br ><br >";

 

$cub = cube ( $num );

 

echo " The cube of { $num } is { $cub }<br ><br >";

// calling methods-return in MainFile .php

 

?>

SecondFile.php

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


IncludeFileResult

Attachment: SecondFile.php


How to include external file using PHP?

 

Include Function takes all the matter in the file and includes that matter or content in the current file where the include statement is called. It generate warning in case any problem arises but the script will continue its execution.

 

//Program to show how to use Include Function in PHP.

 

<!DOCTYPE html>

<html>

<body>       

<?php

   include("demo.php");

?>

</body>

</html>

 

The content of demo.php is:

<html>

<body>

<h1>Php is Server-Scripting Language!!</h1>

</body>

</html>

 

/*Output of the above program is:

Php is Server-Scripting Language!!

*/

 

Attachment: include.php
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud