Home > Article > Backend Development > PHP4 User Manual: Process Control-require_PHP Tutorial
TABLE border=0 cellPadding=0 cellSpacing=0 height="100%" width="100%"> The
require()
require() statement replaces itself with the file it specifies, much like C Preprocessing in #include functionality.
If the "URL fopen wrappers" option is turned on in PHP (which is the default configuration), you can use the URL in require() instead of the local path. See Remote files and fopen() for more information.
An important tip is: how does it work when a file is include() or require()? Start analyzing the PHP module and HTML module bit by bit from the target file, and re-summarize the PHP module at the end.
The premise is that there is some executable PHP code in the file that is contained by valid PHP start and end tags.
require() is not actually a PHP function: rather, it is an integral part of the language. Its rules are somewhat different from those of functions. For example, require() will not be controlled by the containing control structure. Additionally, it does not return any value; attempting to read a return value from a require() call results in a parsing error.
Unlike include(), require() will always read the target file, even if it has no executable lines. If you want to conditionally include a file, use include(). Conditional statements do not affect require().
However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.
Similarly, loop control will not affect the behavior of require().
Although the included file is still controlled by the loop, require() is only executed once.
This means that you cannot put the require() statement inside a loop and expect it to include a different file each time through the loop. To do this, use the include() statement.
require ('header.inc');
When a file is included by require(), the included code will inherit the variable scope of the line where require() occurs. Any variables available on the calling file line will be available in the called file. If require() occurs in a function in the calling file, all the code in the called file will appear to have been defined in the function.
If the file contained in require() was opened via HTTP using fopen, and if the target server can parse the target file as PHP code, the variables can be passed using require() with the HTTP GET request string with the URL. Strictly speaking, the variable scope of require() a file and the parent file that inherits it are different: the script is actually run on the remote server, and its results are included in the local script.
/* This example assumes that someserver has been configured to parse .php instead of .txt files.
* Likewise, 'works' means that the variables $varone and $vartwo are available in the included file */