Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

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

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

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.

Become a Skilled Web Developer in Just 9 Months!

Caltech PGP Full Stack DevelopmentExplore Program
Become a Skilled Web Developer in Just 9 Months!

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

<?php

echo "<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="https://www.google.com">Google</a> |

<ahref="https://www.yahoo.com">Yahoo</a> |

<ahref="https://www.maps.com">Maps</a> | 

<ahref="https://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 Development-MEANExplore 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

<?php

echo "The sub file."; 

?>

Call.php

<?php

//include Main.php file

include("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:

<?php

echo "HELLO,hi";

//require main.php file

require("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.

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

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 Postgraduate Program in Full Stack Web Development. 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 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!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.