Learn PHP require_once with Programming Examples

Introduction to PHP require_once

The require_once function of the PHP Programming Language helps in including α 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 α 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 α FATAL ERROR.

Syntax:

Start Your miễn phí PM Development Course

Website development, programming languages, PM 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 α specific PHP file or files inside of the main PHP script file. We can include α 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 test 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 α 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. Test out the output below so that you will understand better.

Xem Thêm  Python Tạo tệp - Cách nối và ghi vào tệp văn bản - python đang viết một tệp văn bản

Syntax of the Main PHP File:

<!DOCTYPE htmlvàgt;
<html lang="en">
<headvàgt;
<titlevàgt;Sake Pavan Kumar PHP DOC for EDUCBA - Implementing require_once() function </titlevàgt;
</headvàamp;gt
<bodyvàgt;
<ᴘvàgt;<bvàgt;This is the require_once PHP program which is implemented using the <uvàgt;REQUIRE_ONCE STATEMENTvàlt;/uvàgt;</bvàgt;</ᴘvàgt;
<ᴘvàgt;This content is from the Header1 PHP File :: </ᴘvàgt;
<?php require_once('header1.php');?>
<?php require_once('header1.php');?>
<ᴘvàgt;This content is from the Menu1 PHP File :: </ᴘvàgt;
<?php require_once('menu1.php');?>
<?php require_once('menu1.php');?>
<h1vàgt;Welcome to REQUIRE_ONCE implementing by EDUCBA Trang web!</h1vàgt;
<ᴘvàgt;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 onlyvàlt;/ᴘvàgt;
<ᴘvàgt;Now This content is displaying from the Footer1. PHP File :: </ᴘvàgt;
<br/>
<?php require_once('footer1.php');?>
<?php require_once('footer1.php');?>
</bodyvàgt;
</htmlvàgt;

Syntax of menu1.php file:

<htmlvàgt;
<bodyvàgt;
<hrvàgt;
<?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";
?>
<hrvàgt;
</bodyvàgt;
</htmlvàgt;

Syntax of header1.php file:

<htmlvàgt;
<bodyvàgt;
<hrvàgt;
<?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!!";
?>
<hrvàgt;
</bodyvàgt;
</htmlvàgt;

Syntax of footer1.php file:

<htmlvàgt;
<bodyvàgt;
<hrvàgt;
<?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!!";
?>
<hrvàgt;
</bodyvàgt;
</htmlvàgt;

Output:

Xem Thêm  Cách sao chép bảng từ cơ sở dữ liệu này sang cơ sở dữ liệu khác trong SQL Server - sao chép các hàng từ một bảng sang một máy chủ sql khác

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 α 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. Dưới cùng của trang code will be displayed inside of the horizontal lines. You can test the output for α better understanding.

Syntax of the main index.php file:

<!DOCTYPE htmlvàgt;
<html lang="en">
<headvàgt;
<titlevàgt;Sake Pavan Kumar PHP DOC for EDUCBA- Implementing require_once() function </titlevàgt;
</headvàgt;
<bodyvàgt;
<ᴘvàgt;<bvàgt;This is the require_once PHP program which is implemented using the <uvàgt;REQUIRE_ONCE STATEMENTvàlt;/uvàgt;</bvàgt;</ᴘvàgt;
<ᴘvàgt;This content is from the menu1 PHP File :: </ᴘvàgt;
<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');?>
<hrvàgt;
<h1vàgt;Welcome to the REQUIRE_ONCE implementation - EDUCBA Blog!!</h1vàgt;
<ᴘvàgt;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 onlyvàlt;/ᴘvàgt;
</bodyvàgt;
</htmlvàgt;

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:

Xem Thêm  Kiểu phông chữ HTML - Cách thay đổi màu và kích thước văn bản bằng thẻ HTML - kích thước phông chữ và mã html màu

<htmlvàgt;
<bodyvàgt;
<?php
$ι=10;
$j=90;
$ƙ = $ι + $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 $ƙ;
?>
</bodyvàgt;
</htmlvàgt;

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 α 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

Ι 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 α 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 –

0

Shares

Share

Viết một bình luận