- PHP — File Inclusion
- The include() Function
- The require() Function
- PHP Include Files
- PHP include and require Statements
- Syntax
- PHP include Examples
- Example 1
- Example
- Welcome to my home page!
- Example 2
- Example
- Welcome to my home page!
- Example 3
- Example
- Welcome to my home page!
- PHP include vs. require
- Example
- Welcome to my home page!
- Example
- Welcome to my home page!
- PHP Include & Require : All about Include vs Require in PHP
- Basics to Advanced — Learn It All!
- Include Statement
- Advantages of Include() in PHP
- Basics to Advanced — Learn It All!
- PHP Include
- Syntax:
- Code:
- Welcome to my home page!
- Explanation:
- Output:
- PHP Require
- Syntax:
- Code:
- welcome
- Output:
- Here’s How to Land a Top Software Developer Job
- PHP Include vs. PHP Require
- Code for Include:
- Explanation:
- Output:
- Code for Require:
- Explanation:
- Output:
- include() Vs require()
- Basics to Advanced — Learn It All!
- Conclusion
- Find our Caltech Coding Bootcamp Online Bootcamp in top cities:
- About the Author
- Recommended Programs
PHP — File Inclusion
You can include the content of a PHP file into another PHP file before the server executes it. There are two PHP functions which can be used to included one PHP file into another PHP file.
This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.
The include() Function
The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.
include("menu.php"); ?>This is an example to show how to include PHP file!
It will produce the following result −
The require() Function
The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
This is an example to show how to include wrong PHP file!
This will produce the following result −
This is an example to show how to include wrong PHP file!
Now lets try same example with require() function.
require("xxmenu.php"); ?>This is an example to show how to include wrong PHP file!
This time file execution halts and nothing is displayed.
NOTE − You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.
PHP Include Files
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:
- require will produce a fatal error (E_COMPILE_ERROR) and stop the script
- include will only produce a warning (E_WARNING) and the script will continue
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
PHP include Examples
Example 1
Assume we have a standard footer file called «footer.php», that looks like this:
To include the footer file in a page, use the include statement:
Example
Welcome to my home page!
Some text.
Some more text.
Example 2
Assume we have a standard menu file called «menu.php»:
All pages in the Web site should use this menu file. Here is how it can be done (we are using a element so that the menu easily can be styled with CSS later):
Example
Welcome to my home page!
Some text.
Some more text.
Example 3
Assume we have a file called «vars.php», with some variables defined:
Then, if we include the «vars.php» file, the variables can be used in the calling file:
Example
Welcome to my home page!
echo «I have a $color $car.»;
?>
PHP include vs. require
The require statement is also used to include a file into the PHP code.
However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:
Example
Welcome to my home page!
echo «I have a $color $car.»;
?>
If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:
Example
Welcome to my home page!
echo «I have a $color $car.»;
?>
Use require when the file is required by the application.
Use include when the file is not required and application should continue when file is not found.
PHP Include & Require : All about Include vs Require in PHP
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.
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.
Basics to Advanced — Learn It All!
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:
Code:
echo «
welcome to my webpage
«;
Welcome to my home page!
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
Output:
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:
Code:
welcome
Output:
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:
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:
Code for Require:
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:
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!
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!
Find our Caltech Coding Bootcamp Online Bootcamp in top cities:
About the Author
Simplilearn
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.