The ‘include’ (or require) statement copies all of the text, code, and mark-up from the defined file into the include statement's target file. When you want to use the same PHP, HTML, or text on different pages of a website, including files comes in handy.

Include in PHP helps one build various functions and elements that can be reused through several pages. Scripting the same feature through several pages takes time and effort. This can be avoided if we adopt and use the file inclusion principle, which allows us to combine several files, such as text or codes, into a single program, saving time and effort.

PHP Include helps to include files in various programs and saves the effort of writing code multiple times. If we want to change a code, rather than editing it in all of the files, we can simply edit the source file, and all of the codes will be updated automatically. There are two features that assist us in incorporating files in PHP.

  1. include
  2. require

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Include Statement

The ‘include’ or ‘require’ statement can be used to insert the content of one PHP file into another PHP file (before the server executes it). Except in the case of failure, the ‘include’ and ‘require statements’ are identical:

  • Include in PHP will only generate an alert (E_WARNING), and the script will proceed. 
  • Require will produce a fatal error (E_COMPILE_ERROR) and interrupt the script.

If the include statement appears, execution should continue and show users the output even if the include file is missing. Otherwise, always use the required declaration to include the main file in the flow of execution while coding Framework, CMS, or a complex PHP program. This will help prevent the application's protection and reputation from being jeopardized if one key file is corrupted.

The include() function copies all of the text from a given file into the file that uses the include function. It produces an alert if there is a problem loading a file; however, the script will still run.

Advantages of Include() in PHP

  • Code Reusability: We may reuse HTML code or PHP scripts in several PHP scripts with the aid of the ‘include’ and ‘require’ build.
  • Easy to Edit: If you want to alter anything on a website, you can modify the source file used with all of the web pages rather than editing each file individually.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

PHP Include

Include is a keyword to include one PHP file into another PHP file. While including the content of the included file will be displayed in the main file. The below example code will demonstrate the concept of PHP include.

Syntax:

include 'file_name';

or

require 'file_name';

Code:

Page1.php<?phpecho "<p>welcome to my webpage</p>";?>Main.php<html><body><h1>Welcome to my home page!</h1><p>Some text.</p><p>Some more text.</p><?php include 'Page1.php';?></body></html>

Explanation:

In the above code, there are two files, that is, Page1.php and Main.php. In the Main.php file, the Page1.php has been included with the help of line <?php include ‘Page1.php’;?>

Output:

PHP_Include_1

PHP Require

The PHP require function is similar to the include function, which is used to include files. The only difference is that if the file is not found, it prevents the script from running, while include does not.

The require() function copies all of the text from a given file into the file that uses the include function. The require() function produces a fatal error and stops the script's execution if there is a problem loading a file. So, apart from how they treat error conditions, require() and include() are identical. Since scripts do not execute if files are missing or misnamed, the require() function is recommended over include().

Syntax:

require 'file_name'; 

Or  

require ('file_name'); 

Code:

Menu1.html:<html><body><ahref="http://www.google.com">Google</a> |<ahref="http://www.yahoo.com">Yahoo</a> |<ahref="http://www.maps.com">Maps</a> | <ahref="http://www.tutorial.com">Tutorial</a></body></html>Main.html:<html><body><h1>welcome</h1><?php require("menu1.html"); ?></body></html>

Output:

PHP_Include_2 

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

PHP Include vs. PHP Require

The terms "include" and "require" are interchangeable. Include allows the script to proceed if the file is missing or inclusion fails, but require causes the script to halt, resulting in a fatal E_COMPILE_ERROR level error.

Code for Include:

Main.php<?phpecho "The sub file."; ?>Call.php<?php//include Main.php fileinclude("Main.php");echo "The welcome file is included.";?>

Explanation:

The Main.php file isn't in the same directory as the other files we've included. As a result, it will issue an alert about the missing file while also showing the production.

Output:

PHP_Include_3

Code for Require:

<?phpecho "HELLO,hi";//require main.php filerequire("main.php");echo "The welcome file.";?>

Explanation:

The Main.php file isn't in the same directory as the other files we've included. As a result, it will issue an alert about the missing file while also showing the production.

Output:

PHP_Include_4

include() Vs require()

In most cases, the require() statement works in the same way as the include() statement. The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require() statement generates a fatal error and terminates the script.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Conclusion

The “include in PHP” helps one generate various elements and functions that are reused across several pages. Scripting these functions in several pages takes a long time. As a result, using the principle of file inclusion allows you to include files in different applications without having to write code multiple times. 

We hope you found the information in this article useful. Many of the top professionals in the industry have chosen Simplilearn to enhance their careers. To become successful professionals/entrepreneurs, join Simplilearn’s Full Stack Developer - MERN Stack. This Post Graduate Program will help you boost your career as a software engineer. In just a few months, you'll grasp modern coding techniques, and you'll have everything you need to become a Full Stack Java Developer

You can also start studying other in-demand courses for free! Explore all of the free courses, free career guides, interview tips and techniques, and much more with Simplilearn.

In case you have any questions for us, leave them in the comments section below, and our experts will get back to you!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449