Include and Require
Include and Require
include 'filename';
or
require 'filename';
Menu.php
<?php
echo '<a href="/default.asp">Home</a> -
<a href="/html/default.asp">HTML Tutorial</a> -
<a href="/css/default.asp">CSS Tutorial</a> -
<a href="/js/default.asp">JavaScript Tutorial</a> -
<a href="default.asp">PHP Tutorial</a>';
?>
<html>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
</body>
</html>
Assume we have a file called "vars.php", with some variables
defined:
<?php
$color='red';
$car='BMW';
?>
Then, if we include the "vars.php" file, the variables can
be used in the calling file:
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php include 'vars.php';
echo "I have a $color $car.";
?>
</body>
</html>
The require statement is also used to include a file into the
PHP code.