News:

Alert!!! HTML Evaluation 01- Quiz

Main Menu

PHP Include Files: 011

Started by Cohort Master, Apr 01, 2024, 07:56 AM

Previous topic - Next topic

Cohort Master

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.

PHP include and require Statements
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

1. require will produce a fatal error (E_COMPILE_ERROR) and stop the script
2. include will only produce a warning (E_WARNING) and the script will continue


Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Syntax
include 'filename';

or

require 'filename';

PHP include Examples
Example
Assume we have a standard footer file called "footer.php", that looks like this:
<html>
<body>

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

</body>
</html>

QuoteCREATIVITY is one of the facets of web development.