PHP Include and Require Funcation

WBOY
Release: 2024-08-29 12:56:39
Original
271 people have browsed it

The PHP Include and Require statement helps in taking all the code/text/markup code which exists in the specific PHP file/doc and then it will copy the code text from the file included to the file where Include statement of the PHP is used. If the script which contains in the include.php file is copied to the original PHP file then the output will be the same as mentioned in the above description. To differentiate the big project’s programs one must need to create the different programs. To assemble all those files in one, one should use include or require statement in the original index or other PHP Program File.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

include("Enter File Path – Pavan Kumar Sake"); or include "Enter File Path – Pavan Kumar Sake";
require("Enter the File Path – Pavan Kumar Sake"); or require "Enter the File Path – Pavan Kumar Sake";
Copy after login

How does include and require work in PHP?

This statement will help in allowing the code which present in the other.PHP file which contains in a specific original PHP file. It returns the same output if we used the code present in a specific.PHP file code in our original PHP file directly. By using the include() or the require() function/statement one can save lots and lots of time and can reduce the workload too by including the files/content.

Just store some code in a specific.PHP file and then include it in any other PHP file/files whenever you needed just with the help of the include() or require a () statement as mentioned in the Syntax section. Instead of typing the whole PHP code many times when required, one can enter the code using the include() statement multiple times. For example, we can include the HEADER.PHP, MENU.PHP, FOOTER.PHP files in one main INDEX.PHP file to include the code present in those.PHP files in the INDEX.PHP file.

Examples to Implement PHP Include and Require

Below are the examples mentioned:

Example #1

This is the example of implementing the PHP Include statement. At first html tags are created and then in the title tag, some string content is entered. This will be visible on the browser tab when the cursor is placed. Then paragraph “

” tag is used to enter some content and then include statement is used to import HEADER1.PHP file’s content in the index program. Then paragraph tags are used for some content display and then again include statement is used to import MENU1.PHP file’s content. Then header tags and paragraph tags are used with some content in it. Then again PHP includes statement is used to import content from the FOOTER1.PHP FILE. Then the html tag is closed. Include statements of this program that will help in getting the other 3 PHP files content.

Syntax of INDEX.PHP FILE :

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sake Pavan Kumar DOC - Implementing include() function </title>
</head>
<body>
<p>This content is from the Header1 PHP File :: </p>
<?php include "header1.php"; ?>
<p>This content is from the Menu1 PHP File :: </p>
<?php include "menu1.php"; ?>
<h1>Welcome to Our Blog/Website!</h1>
<p>Hi, This is Pavan Kumar Sake. I am a Content Writer, Blogger,
Engineer, Physics Nerd, Tech Enthusiast.
This content is from the original file only</p>
<p>This content is from the Footer1 PHP File :: </p>
<br/>
<?php include "footer1.php"; ?>
</body>
</html>
Copy after login

Syntax of HEADER1.PHP FILE:

<html>
<body>
<hr>
<?php
echo "Now you are in the Header.PHP files content!!"
?>
<hr>
</body>
</html>
Copy after login

Syntax of MENU1.PHP FILE:

<html>
<body>
<hr>
<?php
echo "Now you are in the Menu.PHP files content!!"
?>
<hr>
</body>
</html>
Copy after login

Syntax of FOOTER1.PHP FILE:

<html>
<body>
<hr>
<?php
echo "Now you are in the Footer.PHP files content!!"
?>
<hr>
</body>
</html>
Copy after login

Output:

PHP Include and Require Funcation

Example #2

This is the example of implementing the REQUIRE STATEMENT OF PHP Programming Language. Here instead of using the include statement “include() statement” we are using the required statement “require() statement”. Here also just like the example 1, most of the content is the same with the same HTML tags, and some of the string output content also the same but the difference is only the usage of the REQUIRE STATEMENT OF PHP. Check out the output for a better understanding.

Syntax of INDEX.PHP:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sake Pavan Kumar DOC - Implementing include() function </title>
</head>
<body>
<p><b>This is the PHP program which is implemented using the <u>REQUIRE STATEMENT</u></b></p>
<p>This content is from the Header1 PHP File :: </p>
<?php require "header1.php"; ?>
<p>This content is from the Menu1 PHP File :: </p>
<?php require "menu1.php"; ?>
<h1>Welcome to Pavan Kumar Sake Website!</h1>
<p>Hi, This is Pavan Kumar Sake. I am a Content Writer, Blogger,
Engineer, Physics Nerd, Tech Enthusiast.
This content is from the original file only</p>
<p>This content is from the Footer1 PHP File :: </p>
<br/>
<?php require "footer1.php"; ?>
</body>
</html>
Copy after login

Syntax of HEADER1.PHP:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require statement of PHP..";
echo "Now you are in the Header.PHP files content!!";
?>
<hr>
</body>
</html>
Copy after login

Syntax of MENU1.PHP:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require statement of PHP..";
echo "Now you are in the Menu.PHP files content!!";
?>
<hr>
</body>
</html>
Copy after login

Syntax of FOOTER1.PHP:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require statement of PHP..";
echo "Now you are in the Footer.PHP files content!!";
?>
<hr>
</body>
</html>
Copy after login

Output:

PHP Include and Require Funcation

Advantages to PHP Include and Require

The PHP include and require a statement that is having a huge advantage in using to develop static and dynamic web pages according to our needs. This PHP statement reduces the workload and helps in saving lots of time most of the time. If there is a need to enter some type of code in a specific file or in different types of PHP files one must type each and every time and then if we use include() or require() whenever or wherever you want we can save the time of typing and also can reduce the workload. We can use separate PHP files for each and every web development section.

Conclusion

I hope you learned what is the definition of PHP Include and Require statement along with its syntax and explanation, How to Include and to Require works in PHP along with various examples, Advantages of the statements, etc. to understand the PHP Include or require statements better and so easily.

The above is the detailed content of PHP Include and Require Funcation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template