HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it intothe 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
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.
require
will produce a fatal error (E_COMPILE_ERROR) and stop the scriptinclude
will only produce a warning (E_WARNING) and the script will continueSo, 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
Example 1
Assume we have a standard footer file called "footer", that looks like this:
To include the footer file in a page, use the include
statement:
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer';?>
</body>
</html>
Example 2
Assume we have a standard menu file called "menu":
All pages in the Web site should use this menu file. Here is how it can be done (we are using a <div> element so that the menu easily can be styled with CSS later):
<html>
<body>
<div class="menu">
<?php include 'menu';?>
</div>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
</body>
</html>
Example 3
Assume we have a file called "vars", with some variables defined:
Then, if we include the "vars" file, the variables can be used in the calling file:
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php include 'vars';
echo "I have a $color $car.";
?>
</body>
</html>
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:
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php include 'noFileExists';
echo "I have a $color $car.";
?>
</body>
</html>
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:
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php require 'noFileExists';
echo "I have a $color $car.";
?>
</body>
</html>
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.
Write a correct syntax to include a file named "footer".
<?php ;?>