PHP: File loading
Php file loading overview:
There are four File loading syntax forms (note, not a function): include, include_once, require, require_once;
They are essentially the same and are used to load (introduce/include/load) an external file to the current php code.
They have only two minor differences:
1: If File loading fails, there are different processing rules. Different from require, include
2: If the load is repeated, there are different processing rules. XXX is different from XXX_once.
3: they are all syntax structures, rather than functions, and can be used in two forms:
Include 'file path ';
Include ('file path ');
File path:
No matter which syntax structure is loaded, the file path problem is involved. There are three main cases:
1: relative path
Is the path starting with "./", or "./".
./Indicates the location of the current webpage File (Folder/directory );
../Indicates the upper-level location of the current webpage File (Folder/directory );
This relative position is effective for all contents of a website (including php, html, images, css, and js files ).
2: absolute path
There are two absolute paths:
Absolute local path:
Window System: c:/d1/d2/index. php
Unix:/d1/d2/index. php
Absolute network path: http://www.abc.com/d1/d2/index.php
3: only file names (no path, only file names are provided, not recommended)
In the include syntax of php (the other three are also the same), if only the file name is given, but the path is not given, then the rule for finding the file internally is as follows:
First, search in the include directory set by the system.
In the php. ini configuration file, there are: include_path settings
It can be seen that there is no setting by default. In fact, you can set multiple directories separated by semicolons. The system will search for them in order.
If not found, find it in the directory where the webpage file is located (current working directory)
If not found, find it in the directory of the file where the current include Command is located.
File loading process:
Exit the php script mode from the include statement (enter the html code mode)
Load the code in the file set by the include statement and execute it (as in the current file)
Exit html mode and enter php script mode again.
Several differences:
Difference between include_once and include: the former can ensure that it will not be repeatedly loaded
Difference between require and include: if the included file does not exist, that is, when the reference fails (error), include warning and continue to execute the subsequent code, require will directly terminate
The difference between require_once and require: The former can ensure that it will not be repeatedly loaded.
Return keyword:
Previously, we only showed the return keyword in the function. its function is to end the function and return data;
Return; // end simply;
Return $ x; // end and return data $ x;
Then, the return in the included (loaded) file also plays the same role!
By default, if the include operation succeeds, 1 is returned. if the loading fails, false is returned.
However, in the contained file, you can use the return statement to return the data value and terminate the subsequent execution of the file.
The data returned by return can be further processed like the function return value (for example, assigning values to other variables)