Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn PHP Include Files



PHP Include Files

Including PHP files is a way to modularize your code and reuse functions, classes, or variables across multiple PHP files. It's commonly used for separating logic into manageable chunks and promoting code reusability.

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.



PHP include and require Statements

In PHP, include and require are statements used to include and evaluate external PHP files within another PHP file. The main difference between them lies in how they handle errors:



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. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

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

Assume we have a standard footer file called "footer.php", that looks like this:


Example

<?php
echo "<p>Copyright &copy; 1999-" . date("Y") . " Q3Schools.com</p>";
?>


To include the footer file in a page, use the include statement:


Example

<html>
<body>

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

</body>
</html>


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java