Include & Require PHP - What differences do they have

It’s normal that several pages of a site or Web application contain common elements that are repeated over and over again over dozens of pages.

Include & Require PHP - What differences do they have | Learn PHP & MySQL | It’s normal that several pages of a site or Web application contain common elements that are repeated over and over again over dozens of pages

Copying and pasting whole files | The include and require puzzles

It is normal that several pages of a site or Web application contain common elements that are repeated over and over in dozens of pages.

For example, the same page header, the same navigation menu, or the same footer.

In those cases, it would be impractical if the HTML code of those blocks were repeated identically in each of the numerous HTML files of the site, since when it comes time to modify the content of any of these elements (add a new button in the menu, or a simple change of telephone in a footer), it will be necessary to make the change in each one of the files that show that block, and then we will be forced to upload by FTP to the server perhaps tens, or hundreds, or thousands of files.

To solve this problem, PHP has four "constructions" (they are not functions) called:

  1. include
  2. require
  3. include_once
  4. require_once

The idea when using them is that we will place, in a separate file, the contents that many pages have in common.

For example, we will place, in a file called menu.php, the necessary source code to show the menu, the code of the footer will be placed in another separate file called pie.php, and so on with all the elements common to several pages.

Then, on each page of the site where we need to show that menu or that foot, we order the PHP interpreter software to include the complete code of the file in question in the exact place where we specified it.

This order automatically performs a task similar to the one we would do manually if we select the code of those external files (menu.php, pie.php, etc.), we will copy them and paste them later in each of the pages in which we want to show that foot or that menu.

In this way, when it is necessary to make a change to one of those files, we will do so in the file that contains exclusively the menu code, or the footer, and once the content changes of those unique files are finished, we will upload them by FTP, and we will not have to change anything in the other tens, or hundreds, or thousands of pages that make up the site, which will reflect the changes immediately, since in them it only says "show here what is in menu.php".

The code of this file pagina.php -which simulates being one of the many standard pages of the site- would be as follows.

<html>
<head>
<title>Page that includes other files</title>
</head>
<body>
<div id=”container”>
<?php require (“header.php”); ?>
<div id=”main-content”>
Main Content
</div>
<?php include (“buttons.php”); ?>
<?php include (“footer.php”); ?>
</div><!-- Closure of the container -->
</body>
</html>

And, we would also have a file for each block of page that we want to make independent:

The header.php file:

<div id=”header”>
Header
</div>

The file botones.php:

<div id=”buttons.php”>
Buttons
</div>

The file pie.php:

<div id=”footer.php”>
Foot
</div>

It is clear then that include gives us a huge time saving when it comes to maintenance tasks of a site of many pages.

Differences between include and require are minimal, they are simply differentiated by the type of error they generate if they fail to include a file (for example, if that file does not exist because we delete or rename it).

An include creates in that case, a Warning; this is: it sends a warning on the screen, but it does not interrupt the execution of the rest of the file.

On the other hand, when the order fails, it generates a fatal error, which definitively interrupts at that point the execution of the file that the inclusion attempt was making.

Therefore, in cases where it is absolutely essential to have the data that was stored in the external file that was to be included, it is better to use require (a typical case is external files that contain a user's username and password). data, because if we do not access these key data, it does not make sense to continue the execution of orders that come later, in which case, it would be advisable to use require).

And what is the difference between require and require_once, or between include_once and include? (note: eleven in English means "only once").

With require_once or include_once, if the file has already been included in the same page before, it will not be reinserted a second time.

This can be useful in cases where the same file could potentially be included more than once during the execution of a code, due to the complexity of the code, and you want to be sure that it is inserted only once, to avoid problems with redefining functions, variable values, etc.

Next, an example that uses require, include and require:

<html>
<head>
<title>Page that includes other files </title>
</head>
<body>
<div id=”container”>
<?php require(“header.php”); ?>
<div id=”main-content”>
Main Content
</div>
<?php include(“buttons.php”); ?>
<?php require(“footer.php”); ?>
</div><!--  container closure -->
</body>
</html>

🤖

ChatGPT Free
Ask questions on any topic

CITE ARTICLE


For homework, research, thesis, books, magazines, blogs or academic articles

APA Format Reference:

Delgado, Hugo. (2019).
Include & Require PHP - What differences do they have.
Retrieved Nov 04, 2024, from
https://disenowebakus.net/en/php-include-require

Participates!

Share it with your friends in Social Networks!

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

CONTINUE LEARNING

IT ALSO DESERVES TO PAY TO VISIT:

Not finding what you need?

Use our internal search to discover more information
Related content:

Would you like to learn more about Web Design?

Meet all the courses and tutorials that we have for you completely free
Learn Web Design
 

Leave your Comment

SPONSOR

Your business can also appear here. More information

Next article: