EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Include and Require

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Include and Require

PHP Include and Require

Introduction to PHP Include and Require

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.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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”;

How Include and Require works in PHP?

This statement will help in allowing the code which presents in 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 “<p>” 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>

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Syntax of HEADER1.PHP FILE:

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

Syntax of MENU1.PHP FILE:

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

Syntax of FOOTER1.PHP FILE:

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

Output:

PHP Include and Require - 1

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>

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>

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>

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>

Output:

PHP Include and Require - 2

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.

Recommended Articles

This is a guide to PHP Include and Require. Here we discuss an introduction to PHP Include and Require with appropriate syntax, advantages and examples. You can also go through our other related articles to learn more –

  1. PHP String Operators
  2. PHP wordwrap()
  3. PHP is_null()
  4. PHP explode()

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More