Reference File learning notes for getting started with PHP. Referenced files are different from files referenced in asp in php. I will introduce using require and include to reference file instances in php. Referencing a file is one of the major characteristics of PHP, which is different from referencing a file in asp in php, the following describes how to use require and include to reference file instances in php.
Referencing a file is one of the major characteristics of PHP. this method can put common functions and functions in one file. when other pages need to use these functions or functions, you can call these functions directly by referencing this file. If it is not referenced, writing 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 subsequent maintenance and secondary development.
PHP uses two methods to reference files. the functions used are require () and include (). The effects of the two types of references are the same, but the two functions are different: if require does not return any value when referencing the file, an error will be fatal and the program will stop executing; when using this function for reference, make sure that the code is correct. When the include file references an inverse value, it continues to execute the subsequent code when an error occurs. Therefore, we recommend that you try to use the first function require to reference the file. there is no reverse return value, and the speed and efficiency are faster than include. Usually, require is placed at the beginning of the PHP program. before executing the PHP program, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages.
| The code is as follows: |
|
|
Require ('SQL. php'); // This function is usually placed at the beginning, for example, the file that references the SQL database connection function.
Echo 'example of referencing Files ';
Include ('Hello-world. php'); // This function is generally placed in the processing section of process control.
?> |
Someone may ask, when a page references multiple files, and these referenced files also reference one or more identical files, sometimes there is no need to reference so many times, so how can we make PHP reference only once? Of course, PHP also has a corresponding method, that is, adding a "suffix" Declaration on the basis of the original function, that is, converting the function into require_once () and include_once () respectively, as shown in the following example:
| The code is as follows: |
|
|
Require_once ('SQL. php'); // declare to reference only the SQL. php file once
Echo 'example of referencing Files ';
Include_once ('Hello-world. php'); // declare to reference only the hello-world.php file once
?> |
References and include references to file instances. Referencing a file is one of the highlights of PHP...