PHP Inclusion Constructions

Lecture



Inclusion constructs allow you to build a PHP program (script) from several separate files.

In PHP, there are two basic constructs of inclusions: require and include .

Require inclusion design

The require construct allows you to include files in a PHP script before PHP script execution. The general require syntax is:

require имя_файла;

When launching (exactly at startup, and not while executing!) The program, the interpreter will simply replace the instruction with the contents of the file filename (this file may also contain a PHP script, framed, as usual, with <? And ?> Tags ). And he will do it immediately before launching the program (as opposed to include , which is discussed below). This is quite handy for including various template pages with HTML code in the output of the script. Let's give an example:

Файл header.html:
<html>
<head><title>It is a title</title></head>
<body bgcolor=green>

Файл footer.html:
&copy; Home Company, 2005.
</body></html>

Файл script.php
<?php
require "header.htm";
// Сценарий выводит само тело документа
require "footer.htm";
?>

Thus, the require construction allows you to build PHP scripts from several separate files, which can be either html pages or php scripts.

The require construction supports the inclusion of deleted files (since PHP 4.3.0). For example:

<? php
// Следующий пример на работает, поскольку пытается включить локальный файл
require 'file.php?foo=1&bar=2' ;
// Следующий пример работает
require 'http://www.example.com/file.php?foo=1&bar=2' ;
?>

! The require construct allows you to include deleted files, if this feature is enabled in the PHP configuration file. Details below.

Inclusion construction include

The include construct is also intended to include files in PHP script code.

Unlike the require construct, the include construct allows you to include files in PHP script code. during run the script. The syntax of the include construct is as follows:

include имя_файла;

Let us clarify the fundamental difference between the require and include constructions on a concrete practical example. Create 10 files with the names 1.txt, 2.txt and so on up to 10.txt, the contents of these files are just decimal digits 1, 2 ... ... 10 (one digit in each file). Create the following PHP script:

<? php
// Создаем цикл, в теле которого конструкция include
for($ i = 1 ;   $ i <= 10 ;   $ i ++)   {
include "$i.txt" ;
}
// Включили десять файлов: 1.txt, 2.txt, 3.txt ... 10.txt
// Результат - вывод 12345678910
?>

As a result, we get a conclusion consisting of 10 digits: "12345678910". From this we can conclude that each of the files was included once right during the loop! If we now put the include require instead, the script will generate a fatal error. Compare the result.

PHP converts the script into an internal representation by analyzing the lines of the script in turn, until it reaches the include construct. When it reaches the include , PHP stops translating the script and switches to the specified file in the include . Thus, due to the similar behavior of the translator, the script performance decreases, especially with a large number of files included with include . There are no such problems with require, since the files with require are included before the script is executed, that is, at the time of translation, the file is already included in the script.

Thus, it is more expedient to use the require construction where it is not necessary to dynamically include files in the script, and use the include construct only for the purpose of dynamically including files in the PHP script code.

The include construct supports the inclusion of deleted files (starting with PHP 4.3.0). For example:

<? php
// Следующий пример на работает, поскольку пытается включить локальный файл
include 'file.php?foo=1&bar=2' ;
// Следующий пример работает
include 'http://www.example.com/file.php?foo=1&bar=2' ;
?>

! The include construct allows you to include remote files, if this feature is included in the PHP configuration file. Details below.

The include_once and include_once one-time constructs

In large PHP scripts, include and require statements are used very often. Therefore, it becomes quite difficult to control, as if by accident not include the same file several times, which often leads to an error that is difficult to detect.

PHP provides a solution to this problem. Using the single-entry require_once and include_once constructs, you can be sure that one file will not be included twice. The single inclusion constructions require_once and include_once work in the same way as require and include, respectively. The difference in their work is only in the fact that before turning on the file, the interpreter checks whether the specified file is included earlier or not. If so, the file will not be included again.

! The single inclusion constructs also require_once and include_ince also allow you to include remote files, if this feature is enabled in the PHP configuration file. Details below.

Include remote files

PHP allows you to work with URL objects as with ordinary files. Packers, available by default, are used to work with remote files using the ftp or http protocol.

If the "fopen shell URLs" are included in PHP (as in the default configuration), you can specify the file to be connected using the URL (via HTTP) instead of the local path. If the target server interprets the target file as PHP code, the variables can be transferred to the include file using a URL query string, as in HTTP GET. Strictly speaking, this is not the same as connecting a file and its inheriting the scope of variables of the parent file; because the script runs on a remote server, and the result is then connected to a local script.

In order for remote file inclusion to be available, you need to set allow_url_fopen = 1 in the configuration file (php.ini) .

Please note : PHP versions for Windows prior to PHP 4.3.0 do not support the ability to use remote files with this function even if the allow_url_fopen option is enabled.

<? php

/* Здесь предполагается, что www.example.com сконфигурирован для разбора .php
* файлов, а не .txt файлов. Также 'Works' здесь означает, что переменные
* $foo и $bar доступны в подключённом файле. */

// Не будет работать, так как file.txt не обрабатывается www.example.com как PHP
include 'http://www.example.com/file.txt?foo=1&bar=2' ;

// Не будет работать, поскольку ищет файл 'file.php?foo=1&bar=2' в локальной
// файловой системе.
include 'file.php?foo=1&bar=2' ;

// Следующий пример работает:
include 'http://www.example.com/file.php?foo=1&bar=2' ;

$ foo =   1 ;
$ bar =   2 ;
include 'file.txt' ;    // Работает
include 'file.php' ;    // Работает

?>

See also deleted files, descriptions of the fopen () and file () functions for more information.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)