Home > Article > Backend Development > PHP entry basics reference file study notes_PHP tutorial
There are certain differences between referencing files in PHP and referencing files in ASP. Let me introduce an example of using require and include to reference files in PHP.
Referring files is one of the special features of PHP. This method can put commonly used functions and functions in a file. When other pages need to use these functions or functions, they can directly reference this file. These functions are called. If it is not quoted, rewriting the same function on that page will greatly increase the developer's workload and increase the amount of code in the program, which is not conducive to later maintenance and secondary development.
There are two ways to reference files in PHP. The functions used are require() and include() respectively. The effect of the two references is the same, but there is a difference between the two functions: if require does not return any value when referencing the file, a fatal error will occur and the program will terminate and continue execution; when using this function to make a reference, you Make sure the code is used correctly. When include refers to a file, there is a return value, and it continues to execute the following code when an error occurs. Therefore, it is recommended that you try to use the first function require to reference the file. It returns no value and is relatively faster and more efficient than include. Usually require will be placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.
The code is as follows
|
Copy code
|
||||
echo 'Quote file demonstration'; Include('hello-world.php'); // This function is generally placed in the processing part of process control ?> |
At this time, someone may ask, when a page references multiple files, and these referenced files also reference one or more other identical files, sometimes it is not necessary to reference so many times, then How can I make PHP only reference it once? Of course, PHP also has a corresponding method, which is to add a "suffix" statement to the original function, that is, change the functions into require_once() and include_once() respectively, as shown in the following example: