Up to this point, we have dealt with only one PHP script. However, PHP scripts can be connected to other PHP scripts using two constructs:
require. Both take a single argument – the path to the included file, and the result of their action is the substitution of the contents of the file in the place of their call in the source script. If the PHP script is used as the included script, then it is first substituted into the original script, and then the resulting script is interpreted (Listing 3.12).
Listing 3.12. Use include instructions. Include.php file
<?php echo 'Main script<br />'; include 'included.php'; echo 'Main script<br />';
Let the file included.php contain the code shown in listing 3.13.
Listing 3.13. File included.php
<?php echo 'Include file<br />'; ?> <h3>The text does not have to be displayed by the echo operator.</h3>
As a result of running the script from Listing 3.12, the following lines will be displayed in the browser:
Main script Include file The text does not have to be displayed by the echo operator. Main script
The difference between
require lies in their reaction to the absence of an include file. In the case of
include, a warning is displayed, all subsequent code continues to be executed; in the case of
require, if the file cannot be found, the script stops working.