Updated April 18, 2023
Introduction to PHP include method
PHP include method is used to include any variable, file or standard library components which are required at the time of implementation. If PHP include is specified as a statement at the beginning of the code body, then that code will include the entire set of text, code or markup that exists within the file and copies that set of text, code or markup into the other file where the include statement is present. PHP related application will always require an include statement to maintain the relative flow of execution while implementing the set of code.
Syntax
The Syntax is represented as follows :
include 'file_name';
include is the statement for the flow of execution, and file_name is the name of the file that is required.
How include methods work in PHP?
Include method in PHP is used in a similar fashion as other programming languages. So, it has a certain working flow and process, which is as follows:
- include a statement in PHP is used for statement inclusion and evaluation in terms of the specified file path.
- Require statement is also used in conjunction to include statement but still, there are some differences which are useful to clear the misconception between Require statement and include statement.
- The difference is that if the current PHP code makes use of include and the file that needs to be included with the include statement and PHP cannot find it, then a script will get executed continuously in a loop without any halt.
- But if in case a required statement is used in the code implementation, then the statement used for echoing the statement will not get executed because the execution of the statement gets finished once it is present with some error.
- Thus, it is more recommended to use PHP include instead of PHP require because of the mentioned root cause.
- There are some protocol wrappers as well, which are used for packaging the file and then providing the URL being wrapped with protocols that are useful in case the local pathname is present.
- Once the protocol wrapper is triggered to the PHP based application, then the variables present also get interpreted with the server with the help of the target file, including the entire code and variable.
- Include of a file with a return statement is also possible, which will give developers the ability to write the code with the statement and return the desired output.
- Output buffering with string inclusion in a PHP file is also possible.
- Sometimes it is possible to include files with include statement, but sometimes it is missing or not able to trace the actual file path then; in that case, it becomes very tedious to retrieve and debug the actual file to avoid that some acknowledgements in a timely basis are being provided which can be called as E_WARNINGS or E_ERRORS.
- This helps in simplifying the method of debugging with respect to include the method of PHP.
Examples
Here are the following examples mention below
Example #1
This program demonstrates the PHP include statement where the footer_0 file will be included and is used for further references as shown in the output. Assume that the given footer file is asked as a requirement to be included named as footer_1.php. The file will look like this:
<?php
echo "<p>Copyright © 1999-" . date("Y") . " W3Schools.com</p>";
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Hello Everyone</h1>
<p>hope Everything is doing good. </p>
<p>& I am also feeling on the 9th cloud. </p>
<?php include footer_0.php';?>
</body>
</html>
Output:
Example #2
This program demonstrates the inclusion of a variable where the variable assumed will be used to include the variable as shown in the output, and the assumed variable is represented in the file as below:
<?php
$vegetable='carrot';
$color='Red';
?>
Code:
<!DOCTYPE html>
<html>
<body>
<?php
<h1>Hello_Page!</h1>
<?php include 'vars_1.php';
echo "my favourite veggie is carrot $color $car.";
?>
</body>
</html>
Output:
Example #3
This program demonstrates the PHP include method used with code within the function and called wherever required.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
function Car()
{
global $Name;
include 'vars_1.php';
echo "Porsche_X1 $Name $Car";
}
Car();
echo "Name_Of_Car $Name $Car";
?>
</body>
</html>
Output:
Note: The PHP include function involves the inclusion of a current file that will contain the code for execution.
Example #4
This Program Demonstrates the use of a requiring statement where the file does not exist and, since it is not present, gets terminated itself before the execution of the entire code as mentioned earlier and is shown in the output.
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Hello_Page!</h1>
<?php require 'File_Not_Exist.php';
echo "A color is required. $fruit $guava.";
?>
</body>
</html>
Output:
Example #5
This program is used to demonstrate the inclusion of any standard menu file like menu_1.php in the current program, as shown in the output. Representation of the assumed menu-1.php file is as follows :
<?php
echo '<a href="https://cdn.educba.com/default.asp">Main </a> -
<a href="https://cdn.educba.com/html/default.asp">Java_Class </a> -
<a href="https://cdn.educba.com/css/default.asp">C++_Class</a> -
<a href="https://cdn.educba.com/js/default.asp">Python_class</a> -
Code:
<!DOCTYPE html>
<html>
<body>
<div class="menu_1">
<?php include 'menu_1.php';?>
</div>
<h1>Today the weather is pleasant.</h1>
<p>Pleasant weather is always a need.</p>
<p>Hope for the good and pleasant weather always. </p>
</body>
</html>
Output:
Example #6
This program is used to demonstrate the return statement with PHP include statement which typically behaves to proof if a valid variable file is included, then it will return the value true otherwise, it will return a value as false as shown in the program.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
if (include('vars_1.php') == TRUE) {
echo 'It is Working_Fine';
}
if ((include 'vars_1.php') == FALSE) {
echo 'It is not Working_fine';
}
?>
</body>
</html>
Output:
Conclusion
PHP include statement plays a pivotal role in terms of programming as it gives programmers the ability to include and play around with the necessary files and variables or code that are necessary to manipulate and provide proper output as per requirement. PHP include is considered as a useful method in terms of packaging and file or code handling.
Recommended Articles
This is a guide to PHP include. Here we discuss the introduction, syntax, and working of include methods in PHP along with different examples and code. You may also have a look at the following articles to learn more –