Updated April 12, 2023
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:
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
Below are the examples to implement the same:
Example #1
This is an 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 is 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>
<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:
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>
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:
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 this function. This function will reduce the typing effort of typing the code whenever needed.
Conclusion
I hope you learned what is the definition of the 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 illustrate function, Advantages, etc. to understand this 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 –