PHP Include Files

PHP Include Files


When you wish to utilize the same PHP, HTML, or text file on several pages of a website, including files is quite beneficial.

Include files are reusable components.

PHP offers include and require statements which you allow to reuse files in your website.

You can also search for these topics, Include files in php, php include files list, php include files in function, php include from another directory, php include files example, php include files from url, php include files in function.

PHP include and require Statements

The include or require statement can be used to include or import the content of a file into another PHP file (before the server runs it).

The both statements are same or identical but with a little different.

  • The require statement will include the content of specified file if it found, otherwise it will generate a fatal error (COMPILE ERROR) and the script will be terminated.
  • The include statement will include the content of specified file if it found, otherwise it will generate an error (WARNING), and the script will continue to run.

Including files are very useful in many situations. A website can have common things in many pages like:

  • Header
  • Footer
  • Side Menu Links
  • Others

Note:- These above mentioned itesms are used almost every pages in a website.

Syntax :-

include 'filename';
or:-
require 'filename';


You can also search for these topics, include and require statements in php, php statement include_once, php include statement, php require statement, php incude statement example, php require statement example, php include multiple files one statement, php require_once, syntax for include statement using php, syntax for require statement using php, php include and require statement one php file into another php file.

PHP include Examples

Use the include statement if you want the execution to continue and display the results even if the specified file is missing. Otherwise, always utilize the require statement to include a vital file in the flow of execution.

Example 1 :- Assume we have a file called "footer.php" with the following contents :

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

To include the footer.php file in a php page called "demo.php", use the include statement:

<!DOCTYPE html>
<html>
<body>
<h1>Welcome to my page!</h1>
<div class="mainimage"><p class="image">
<img src="php-resources/mimages/php-include-require-1.jpg" style="width:100%; height:100%" alt="Welcome to my page!" />
</p></div>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

Output :-

Welcome to my home page!

Some text.

Some more text.

Copyright © 2012-2024. Simmanchith.com

Note:- You can include not only the contents of a file and also PHP programming variables, functions, and classes.

Example 2 :- Let's assume we have a file called "vars.php" that contains some php variables:

<?php
$color='red';
$car='BMW';
?>

The variables can then be used in any php file if we include "vars.php" file.

<!DOCTYPE html>
<html>
<body>




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














<?php include 'vars.php';
echo "I have a $color $car.";
?>
</body>
</html>

Output :-

Welcome to my home page!

I have a red BMW.";


PHP include vs. require

A file can also be included in PHP code using the require statement.

Example 1 :- There is also a huge distinction between include and require: if the include statement contains a file and PHP cannot find the file, then the script is still running :

<!DOCTYPE html>
<html>
<body>




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














<?php include 'noFileExists.php';
echo "Hi";
?>
</body>
</html>

Output :-

Welcome to my home page!

Hi

Example 2 :- If we use the require statement to utilize the same example, the echo state will not be executed since the script execution terminates after a fatal error was returned by the require statement :

<!DOCTYPE html>
<html>
<body>




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














<?php require 'noFileExists.php';
echo "Hi";
?>
</body>
</html>

Output :-

Welcome to my home page!


Use require when the application requires the file.

When the file isn't needed, use include, and if the file isn't discovered, the application should proceed.



You can also search for these topics, include vs require using php, differentiate include vs require using php, what is the difference between include vs require in php, php include vs require example, php include vs require_once.