The ability to include the contents of one file in another is one of the most powerful features of PHP. It is also one of the easiest to implement.
Most pages on a site have common elements, such as header, footer, and navigation menus. You can change the appearance of these elements throughout the site by changing the style rules in the external style sheet. But CSS has only limited options for changing the content of page elements. If you want to add a new menu item, you need to edit the HTML on each page that displays it. Web development tools such as Dreamweaver and Expression Web have template systems that automatically refresh all pages connected to the main file, but you still need to upload files to the remote server.
This is not necessary if PHP supports server-side inclusion (SSI). Server-side include is an external file containing dynamic code or HTML (or both), which must be included in several pages. In PHP, content is embedded in every web page on the server. Since each page uses the same external file, you can update the menu or other common elements by editing and loading a single file, which significantly saves time.
PHP file inclusion commands
In PHP, there are four commands that can be used to include code from an external file, namely:
They all do basically the same thing, so why are there four of them?
The fundamental difference is that include () tries to continue processing the script even if there is no external file, while require () is mandatory: if the file is missing, the PHP interpreter stops processing and generates a fatal error. In practical terms, this means that you must use include () if the page should remain working even without the contents of an external file. Use require () if the page is dependent on an external file.
The other two commands, include_once () and require_once (), work the same way, but prevent the inclusion of the same file more than once on the page. This is especially important when including files that define functions and classes. Attempting to define a function or class in a script more than once causes a fatal error. Thus, using include_once () or require_once () ensures that functions and classes are defined only once, even if the script tries to include the external file more than once, as might happen if the commands are executed in conditional statements.
So which command to use? I recommend using include () for external files that are not critical, and require_once () for files that contain function and class definitions.
Where does PHP look for include files?
To include an external file, you must pass one of the four include commands the path to the file as a string, in other words, the path to the file must be in quotation marks (single or double, it does not matter). The file path can be absolute or relative to the current document. For example, any of the following lines will work (assuming that the target file exists):
include (‘includes / menu.inc.php’);
include (‘C: /xampp/htdocs/phpsols/includes/menu.inc.php’);
include (‘/ Applications / MAMP / htdocs / phpsols / includes / menu.inc.php’);
PHP accepts slashes in file paths on the Windows operating system.
Using parentheses in inclusion commands is optional, so the following lines will also work:
include ‘includes / menu.inc.php’;
include ‘C: /xampp/htdocs/phpsols/includes/menu.inc.php’;
When using a relative file path, it is recommended that you use ./ to indicate that the path starts in the current folder. Thus, it would be more efficient to rewrite the first example as follows:
include (‘./ includes / menu.inc.php’); // the path starts in the current folder
Using the file path relative to the site root does not work, as in the following example:
include (‘/ includes / menu.inc.php’); // WILL NOT WORK
If the PHP interpreter cannot find the file, it also searches the include_path as defined in the PHP configuration. I will return to this issue later in this chapter. But before that, let’s try using PHP inclusion in practice. For now, I recommend that you use the file path relative to the current document.
Choosing the right file name extension for inclusions
As you just saw, the included file may contain raw HTML. When the PHP interpreter encounters an include command, it stops processing at the beginning of the external file and resumes at the end. If you want the external file to use PHP code, then the code must be enclosed in PHP tags. From this we can conclude that the included file can have any extension.
It is generally accepted to use .inc as a file name extension so that it is clear that the file is intended to be included in another file. However, most servers treat .inc files as plain text. This poses a security risk if the file contains confidential information, such as a username and password for the database. If the file is stored in the root folder of your site, anyone who knows the file name can simply type the URL in the address bar of the browser, and the browser will helpfully show all your secret details!
On the other hand, any file with the .php extension is automatically sent to the PHP interpreter for parsing (parsing) before being sent to the browser. So, while your secret information is inside the PHP code block and in the file with the .php extension, it will not be disclosed. Therefore, many developers use .inc.php as a double extension for PHP inclusions. The .inc extension reminds you that this is an included file, but only the servers are only interested in .php at the end, which ensures that all PHP code is properly processed.
Since it is common practice to store included files in a separate folder, often called includes, you can argue that .inc.php is redundant. Which naming conventions you choose is up to you, but using only .inc is less secure.