Home >Backend Development >PHP Tutorial >PHP Tutorial. Experience and Skills (Part 2)_PHP Tutorial
3. Files are our friends
No matter the size of the website you are developing, you should be aware of the importance of code reuse, whether the code is HTML or PHP code. For example, you must change the footer containing copyright information at least once a year, which is annoying if you have a website with 1,000 pages.
In PHP, there are at least a few functions that can help you achieve code reuse. The functions used depend on the code you are reusing. The main functions are:
* include( ) and include_once()
* require() and require_once()
include() function includes and calculates the given file, for example:
include(/ home/me/myfile);
Any code in the include file is executed within the scope of the code where include() appears. You can include it on your own server by using include() and fopen() together. Static files, containing target files on another server.
The function of include_once() is the same as include(). The difference between the two is that it will check whether the code in a file is already included in the existing script. If the code already exists, it will not Include it again.
The require() function replaces itself with the contents of the given file. This replacement process occurs when the PHP engine compiles the code, not during execution. It does not calculate first like include() . The require() function is more used in static elements, while include() is used more in dynamic elements. Similar to include_once(), require_once() will first check whether the given code has been inserted. If the code already exists, it will not be inserted again.
In order to understand its content, I prefer to use the require function in copyright information, static text and other elements that do not contain variables or rely on other executing scripts. For example: