PHP include and require Statements - ProgrammerTech
Language:

Search

PHP include and require Statements

  • Share this:
PHP include and require Statements

Merge multiple files together

Merge files in PHP, today we will talk about how to merge php files with each other, there are four elements that we can use to merge php files together, and they are:

  • include.
  • require.
  • include_once.
  • require_once.

We will explain to you what is the difference between these four elements that can be used to merge php files together.

 

The include element in PHP

The include element, let's say we have a php file other than the current one and save it to our working directory. For example cs.php, at first we create anything with this file. For example, we will write a variable that prints a text value, for example, Programmer Tech, and we want to copy the code from this file to the first other file.

second file

<?php
$kaissar = "Programmer Tech";
echo $kaissar;
?>

How to merge it into the first file.

<body>
    <?php include 'cs.php'; ?>
</body>

Here the second file is in the same working folder, so we just called it by name directly. If it is in another place, the entire path must be specified, or if it is located in another folder with the same work path. We go back steps by writing two points and slash each step back. And when you run the first file on the server, the result will be:

Print Programmer Tech text from subfile

 

We can also call the variable by its name, for example, we write the echo cs.php command and it will print what is inside that variable from the second file, in short, we can copy everything from one file to another file easily.

 

The require element in PHP

When used in the previous code, it will do the same thing, but the difference between these two is when it causes an error. For example, if we put a merger of another file, but the error was written in the name of the file, for example, we wrote the name of the second file css.php, which means that this file does not exist.

<body>
    <?php require 'css.php';?>
</body>

And when running the code, an error will appear and the site will stop working and specify the line on which this error occurred. When running the code in the server, the error in the name will be in the form.

php server error message

 

As for the use of include and errors in the name of the second file, the error will be shown and the rest of the code will continue to work, unlike require, the entire site will stop working, if we make a mistake in the name of the second file and write another element after the php command, for example.

<body>
    <?php require 'css.php';?>
    <h1>Muhammad Alush</h1>
</body>

When we run the code, we will see the result as.

An error message from the server and a follow-up to the next code

 

We notice that it shows an error, and the program continues to run, showing what's inside the h1 element we created after showing the error.

 

The require_once element in PHP

When using it, it will search the file, whether you previously merged a file with another file or not "with the same name and path". If you have merged before, it will not merge it, and if we have not merged it before, it will merge it.

<body>
<?php require_once 'pr.php'; ?>
<?php require_once 'pr.php'; ?>
<h3>welcome in my website<h3>
</body>

And when you run the code, the result will be.

Print Welcome to our website

 

We also note that he did not merge it twice because he checked the second time and found it merged, so it was not merged.

 

The include_once element in PHP

The same idea, if you merged it previously, it will only merge once, and the second time it will not merge again. When you use Include you can merge the file ten or more times into the same file.

<body>
<?php include 'pr.php'; ?>
<?php include 'pr.php'; ?>
<h3>welcome in my website<h3>
</body>

And when we run this code, we will see the result.

طباعة اسم الموقع مرتين ملتصقات

 

We also note that it has been called and merged twice as many times as we typed the summon command. Suppose we have another folder in the same folder we are working in, we will name it for example sp and create another php file within this folder. If we want to merge one of the files from the first folder and the folder we just created, the merge can be done by going back one step by putting a colon ../ and writing the file name as in the following example.

<body>
<?php include '../cs.php'; ?>
</body>

important note:

In the event that we want to create many html pages in the same site, for example 10 pages, and all the codes within the head tag are similar and the difference is only in the codes in the body of the page. Here we only have to create an html file and put the head tag code within it, and in the rest of the site pages we call it by include or require and specify the path and name of the file.

<!DOCTYPE html>
<html lang="en">
<?php include 'data.php'; ?>
<body>
    <h1>welcome in my website</h1>
</body>
</html>

In the example, we had a file for the head tag and we called it data.php and we called the rest of the site’s pages as in the previous code. We do not write anything about the head tag on the rest of the pages, and any modification or addition to the file containing head codes will be applied to all pages of the site.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com