|
Include() function is similar to Return statement the only difference is that include
function will continue code execution after giving a warning message, but require
will end the code execution after giving a Fatal error message
Include structure is usually used to add headers, footers. In a webpage the header, footer
contents are stored in a file and included in each every webpage files, so that to make changes,
one need not change each and every file.
Files for including are first looked for in the current working directory, and then in the
directory of current script. The included file will inherit the variable scope of the line in
which include is called, but all functions, classes will have a global scope.
test1.php
<?php
echo "<a href='http://www.test.com/index.php'>Home</a>";
echo "<a href='http://www.test.com/about.php'>About Us</a>";
$color = 'red';
$fruit = 'cherry';
?>
test2.php
<?php
echo "A $color $fruit";
include 'test1.php';
echo "A $color $fruit";
?>
Result:
A
Home About Us
A red cherry
In the above example the header is added and also the variable declared, but is displayed only after the include
structure is called in the script.
|