EDUCBA

EDUCBA

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

PHP require_once

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP require_once

PHP require_once

Introduction to PHP require_once

The require_once function of the PHP Programming Language helps in including a PHP file in another PHP file when that specific PHP file is included more than one time and we can consider it as an alternative for include function. If a PHP script file already included inside our required PHP file then that including will be ignored and again the new PHP file will be called just by ignoring the further inclusions. Suppose if a1.php is one of the PHP script file calling b1.php with require_once() function and it don’t fine b1.php, a1.php stops executing and will cause a FATAL ERROR.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

require_once(‘ PHP file with the path ’);

How require_once works in PHP?

The require_once function of the PHP language works only once even though if we include a specific PHP file or files inside of the main PHP script file. We can include a PHP script file/files as many times as we need but only once the specific PHP will be included and executed for the display of the output of the program. This require_once concept works only for PHP 4, PHP 5, PHP 7 versions and may be for PHP 7+ versions it will work. This first check whether the specific .PHP file is already included or not. If already included then the require_once function will leave including same .PHP file. If not, the specific .PHP file will be included. It is a very useful statement of the PHP Programming Language.

Examples to Implement PHP require_once

Below are the examples to implement the same:

Example #1

This is the example of implementing the require_once() function in PHP. Here some string text is entered just for the display purpose inside of the paragraph “<p>” tags and in some other tags. Then require_once() function is used for header1.php file, menu1.php file, footer1.file twice but the code will be implemented only once because of the require_once() functionality. In each and every. PHP file which are used for including has only some string text printed with the help of the echo statement just to know how the require_once() function is working. Check out the output below so that you will understand better.

Syntax of the Main PHP File:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sake Pavan Kumar PHP DOC for EDUCBA - Implementing require_once() function </title>
</head&gt
<body>
<p><b>This is the require_once PHP program which is implemented using the <u>REQUIRE_ONCE STATEMENT</u></b></p>
<p>This content is from the Header1 PHP File :: </p>
<?php require_once('header1.php');?>
<?php require_once('header1.php');?>
<p>This content is from the Menu1 PHP File :: </p>
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<h1>Welcome to REQUIRE_ONCE implementing by EDUCBA Website!</h1>
<p>Hi Buddy, We are from EDUCBA. We have lots of Content Writers, Bloggers,
Engineers, Physics Nerds, Tech Enthusiasts to provide the better content.
This content which is displaying from the original file only</p>
<p>Now This content is displaying from the Footer1. PHP File :: </p>
<br/>
<?php require_once('footer1.php');?>
<?php require_once('footer1.php');?>
</body>
</html>

Syntax of menu1.php file:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require_once statement of PHP..";
echo "Now you are in the menu1.PHP files content!! because of require_once but calling twice don't works with this";
?>
<hr>
</body>
</html>

Syntax of header1.php file:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require_once statement of PHP..";
echo "Now you are in the header1.PHP files content because of require_once but calling twice don't works with this!!";
?>
<hr>
</body>
</html>

Syntax of footer1.php file:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require_once statement of PHP..";
echo "Now you are in the footer1.PHP files content because of require_once but calling twice don't works with this!!";
?>
<hr>
</body>
</html>

Output:

PHP require_once - 1

Example #2

This is also another example of implementing this function. Here in the main original file (index.php) file, require_once() function is used 7 times to include menu1.php. In the menu1.php file, only the header1.php file is included using the method few times. Then in the header1.php file, only footer1.php file a few times and it is to include the content of the footer1.php. Here we didn’t mention any text in the sub.PHP files but at last in the footer1.php file only sum code is implemented and that code will be executed and displayed along with the main original PHP file’s content/code. Footer code will be displayed inside of the horizontal lines. You can check the output for a better understanding.

Syntax of the main index.php file:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sake Pavan Kumar PHP DOC for EDUCBA- Implementing require_once() function </title>
</head>
<body>
<p><b>This is the require_once PHP program which is implemented using the <u>REQUIRE_ONCE STATEMENT</u></b></p>
<p>This content is from the menu1 PHP File :: </p>
<hrgt;
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<hr>
<h1>Welcome to the REQUIRE_ONCE implementation - EDUCBA Blog!!</h1>
<p>Hi Buddy, We have many Content Writers, Bloggers,
Engineers, Physics Nerds, Tech Enthusiasts to provide better content.
This content which is displaying from main index file only</p>
</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,678 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 the menu1.php file:

<?php
require_once 'header1.php';
require_once 'header1.php';
require_once 'header1.php';
?>

Syntax of the header1.php file:

<?php
require_once 'footer1.php';
require_once 'footer1.php';
require_once 'footer1.php';
require_once 'footer1.php';
?>

Syntax of the footer1.php file:

<html>
<body>
<?php
$i=10;
$j=90;
$k = $i + $j;
echo "This content is included from the footer1.php file - Code from footer1.php file";
echo "<br>";
echo "This the sum of $i and $j values :: ";
echo $k;
?>
</body>
</html>

Output:

PHP require_once - 2

Advantages

With the help of this function, we can reuse the HTML code or some PHP script whenever we want at any PHP file. We can use the specific.PHP file only once at a time. One can change anything in the specific file whenever the webpages have to undergo editing, we just have to edit one PHP file code rather than editing all the code every time if we don’t include the files using the same or include() or other function. So the code reusability and easily editable features are the advantages of the PHP require_once() function. This function will reduce the typing effort of typing the code whenever needed.

Conclusion

I hope you learned what is the definition of require_once function of the PHP Programming Language along with its syntax, How the PHP require_once function works along with some examples of which illustrates function, Advantages, etc.. to understand the PHP require_once concept better and so easily.

Recommended Articles

This is a guide to PHP require_once. Here we discuss an introduction to PHP require_once, syntax, how does it work with examples to implement. You can also go through our other related articles to learn more –

  1. PHP File Handling
  2. PHP strtoupper()
  3. PHP file Functions
  4. PHP Interface

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

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 Course Learn More