In PHP, require means "introducing a file" and is generally used to "include and run the specified file", that is, insert useful code written in other files into the execution flow. The syntax "require ' file name'". When the require statement introduces a file, if an error is encountered, a prompt will be given and the subsequent code will stop running.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
require statement can include and run the specified file, use Used to insert useful code written in other files into the execution flow.
For require(), the file is processed only once (in fact, the file content replaces the require() statement). This means that if the code is likely to be executed multiple times, it is more efficient to use require().
require() is used like:
require("myfile.php") require "myfile.php"
Description: The require() statement is a language structure, not a real function. It can be like other language structures in php Same, for example, echo() can use echo("ab") form, or echo "abc" form can be used to output the string abc. The require() statement can also add parameters directly without parentheses.
This statement is usually placed at the beginning of the PHP script program. Before the PHP program is executed, it will first read the file introduced by the require() statement, making it a part of the PHP script file.
Error reporting
require When an error occurs, an E_COMPILE_ERROR level error is generated; that is, when an error is encountered when importing a file, a prompt will be given and the subsequent execution will stop. code.
Create a new test-require.php file and write the following code (do not have a file named test-nothing.php in the directory.)
<?php require 'test-nothing.php'; echo 'abc'; ?>
Browse http://localhost/ test-require.php, because the test-nothing.php file was not found, we saw the error message, but abc was not displayed below the error message. What you saw may be similar to the following:
Warning: require(test-nothing.php) [function.require]: failed to open stream: No such file or directory in D:\www\test-require.php on line 2 Fatal error: require() [function.require]: Failed opening required 'test-nothing' (include_path='.;C:\php5\pear') in D:\www\test-require.php on line 2
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does require mean in php. For more information, please follow other related articles on the PHP Chinese website!