0% found this document useful (0 votes)
9 views12 pages

Include and Require

Uploaded by

sachin248124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Include and Require

Uploaded by

sachin248124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Include () and require() in PHP

include (or require) statement

The include (or require) statement takes all the


text/code/markup that exists in the specified file and copies it
into the file that uses the include statement.
Including files is very useful when you want to include the
same PHP, HTML, or text on multiple pages of a website.
The include and require statements are identical, except upon
failure:
• require will produce a fatal error (E_COMPILE_ERROR) and stop
the script
• include will only produce a warning (E_WARNING) and the
script will continue
Include or require statement

So, if you want the execution to go on and show users


the output, even if the include file is missing, use the
include statement. Otherwise, in case of FrameWork,
CMS, or a complex PHP application coding, always use
the require statement to include a key file to the flow of
execution.
Syntax

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>

<h1>Welcome to my home page!</h1>


<p>Some text.</p>
<p>Some more text.</p>

</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.

However, there is one big difference between include and


require; when a file is included with the include statement
and PHP cannot find it, the script will continue to execute:
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>
</body>
</html>
Output:-
Welcome to my home page!
I have a .
If we do the same example using the require statement, the echo
statement will not be executed because the script execution dies
after the require statement returned a fatal error:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>
</body> Welcome to my home page!
</html>

You might also like