Include in PHP


Prev Tutorial Next Tutorial

Require in PHP

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.

Syntax

include 'filename';

or

require 'filename';

footer.php

<?php

echo "<p>Copyright © 1999-" . date("Y") . " Sitesbay.com</p>";

?>

home-page.php

<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>

</body>
</html>

home-page.php

<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php require 'footer.php';?>

</body>
</html>

Difference between Include and Require

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.

Use include if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed. Using require means your script will halt if it can't load the specified file, and throw a Fatal error.

when a file is included with the include statement and PHP cannot find it, the script will continue to execute. In case of required file can't be execute.


Prev Tutorial Next Tutorial

Advertisements
Download Projects

Advertisements

Download WhatsApp Status Video

Buy This Ad Space Only $50 per Month, Ad Size 300X250 Contact on: [email protected] WhatsApp no. 8882538826