Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

PHP: Require() Function |Difference from include() function


In previous PHP tutorial, we discussed the include() function. Include() function and require() function have only a slight difference.With the help of require() function, you can put the content of any page on another page.

As mentioned in the include() function, you divided page code into different pages like -header.php, sidebar.php, and footer.php. To remove page complexity and time-saving, we create another same code pages for the whole website.

You do not need to change the header from all website pages. You can change only the header page and it reflects in whole website pages. The require() function is also used for kind of operation.

For example -Suppose that we have a website index page. You can put the content from another PHP file using the require() function. We remove the header code from the index file and create another file header.php and same remove sidebar, footer and create another PHP files - sidebar.php, footer.php. We put the content into the index file from the header, sidebar and footer files using require() function . Now you do not need to change whole website pages, just edit the header file and change the content. The header changes reflect on whole website pages.

Require() function in PHP

The require() function is used to the attach (get) all the content from another file. If you want to use another file data on a page so you can use the require () function. If there is any problem in loading a file, then the require() function generates a fatal error and hold the execution of the script.

. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.

Syntax



require("file_name.php");

Example of require() function

Now let's create an example for the required method. Suppose that, you have a website and it has three pages.

1. Index page
2. About Us page
3. Portfolio page
The header and footer content should be the same for all pages. . In this situation, we get content with the help of the require() function in all three pages(In Whole website pages ). Here we can create separate pages for header and footer and include the help of the require () function in all three pages.
1. Remove all codes of header and paste in the new file and save as header.php.
2. Remove all codes of the sidebar and paste in the new file and save as sidebar.php.

Before using require() function

1.The index page contains the code of content. The header and footer have the same code for all pages.

<html>

Header

index body content

footer

</html>

2.The about us page contains the code of content. The header and footer have same code for all pages.

<html>

Header

About Us main body content

footer

</html>

3.The portfolio page contains the code of content. The header and footer have same code for all pages.

<html>

Header

Portfolio main body content

footer

</html>

Use require() function

This is the main step to get remove the similar content from the whole website pages.

First of all, remove the same content code from all these three pages and create new files according to content behave.

1. Cut all header code and paste in new file and save as header.php .


Header

2. Cut all footer code and paste in the new file and save as footer.php .


footer

It is important to put all header and footer content on all website pages using require() function.

1. index.php
2. about_us.php
3.portfolio.php
1.Put all content in index page from the files header and footer using require() function.

<html>
<?php require("header.php"); ?>

index body content

<?php require("footer.php"); ?> </html>

2. Put all content in about us page from the files header and footer using require() function.

<html>
<?php require("header.php"); ?>

about Us main body content

<?php require("footer.php"); ?> </html>

3. Put all content in portfolio page from the file header and footer using require() function.

<html>
<?php require("header.php"); ?>

Portfolio main body content

<?php require("footer.php"); ?> </html>

A website can contain a lot of pages. Now you can change header content from header.php without editing all pages of the website. Just change in header.php file and it reflects all pages of the website. Follow the same for sidebar and footer.

Differnce between include() function and require() function

As you saw in the above example, there is not much difference in the include() function and require () function.
Only one difference between the include() function and Require () function.

Include() function

-

If there is a problem in file loading, the include ()function displays a warning message, and execute the PHP script.

If there is a small error to loading a file using include () function, then the normal message is displayed and the remaining entries are executed.

Require() Function -

If there is an error in file load, then the fatal error is displayed on the blank page and hold the rest of the code executed.


Please Share

Recommended Posts:-