Does php have a file reference method?

青灯夜游
Release: 2023-03-15 18:30:02
Original
2538 people have browsed it

There are 4 ways to reference files: 1. Use the include statement with the syntax "include 'file name'". When an error occurs in the included file, the system will report an error, but the program will continue to execute; 2. Use require Statement, syntax "require 'file name'"; 3. Use include_once statement, etc.

Does php have a file reference method?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

File reference (file inclusion) means to include another file The entire content of a source file is included in the current source file for use, which is often called introducing an external file. Referencing external files can reduce code reusability and is an important skill in PHP programming.

PHP provides 4 very simple but useful reference statements, namely include statement, require statement, include_once statement and require_once statement. There are certain differences in the use of these four statements.

1. Include statement

When using the include statement to include an external file, the external file will only be included when the code is executed to the include statement. When the included external file When an error occurs, the system will give a warning, and the entire PHP program will continue to execute.

The syntax format of the include statement is as follows:

include('filename') 
//或者 
include 'filename'
Copy after login

where filename is the file path to be included (both relative and absolute paths are acceptable), filename is a string, so single quotes are required' ' or wrapped in double quotes " ". At the same time, the brackets after include can also be omitted. When the brackets are omitted, include needs to be separated from the following filename by a space.

For the convenience of demonstration, here we prepare a demo.php file and simply define a $str variable in it, as shown below:

<?php
    $str = &#39;https://www.php.cn/&#39;;
?>
Copy after login

[Example] Use the include statement to include the demo .php file, the code is as follows:

<?php
    include &#39;./demo.php&#39;;
    echo $str;
?>
Copy after login

The running result is as follows:

https://www.php.cn/
Copy after login
Copy after login

2. require statement

How to use the require statement and include The statements are similar and all implement references to external files. Before the PHP file is executed, the PHP parser will replace the require statement with the entire content of the referenced file, then form a new PHP file with other statements except the require statement, and finally execute the program code according to the new PHP file.

Note: Because the require statement is equivalent to completely copying the contents of another source file into this file, it is generally placed at the beginning of the source file to reference the public function files and files that need to be used. Public class files, etc.

The require statement is almost exactly the same as the include statement. The difference is that when the included file does not exist or there is an error, the require statement will issue a Fatal error and terminate program execution, while the include statement will issue a Warining warning. But the program will continue to execute downwards.

The syntax format of the require statement is:

require(filename) 
//或者 
require &#39;filename&#39;
Copy after login

The parameter filename is the path of the file to be included, and its characteristics are the same as the parameters in the include statement.

[Example] Use the require statement to include the demo.php file defined above. The code is as follows:

<?php
    require &#39;./demo.php&#39;;
    echo $str;
?>
Copy after login

The running results are as follows:

https://www.php.cn/
Copy after login
Copy after login

3. include_once statement

The include_once statement is similar to the include statement. The only difference is that if the included file has already been included, it will not be included again. include_once can ensure that the same file is only included once during script execution to avoid problems such as function redefinition and variable reassignment.

Let’s adjust the demo.php file defined above as follows:

<?php
    echo &#39;php中文网<br>&#39;;
    echo &#39;https://www.php.cn/&#39;;
?>
Copy after login

[Example] Use the include_once statement to include the demo.php file, the code is as follows:

<?php
    include_once &#39;./demo.php&#39;;
    include_once &#39;./demo.php&#39;;
    include_once &#39;./demo.php&#39;;
?>
Copy after login

The running results are as follows:

php中文网
https://www.php.cn/
Copy after login
Copy after login

4. require_once statement

require_once statement is an extension of the require statement. Its function is basically similar to the require statement. Different Yes, when applying the require_once statement, it will first check whether the file to be included has been included elsewhere in the program. If so, the file will not be included again.

[Example] Use the require_once statement to include the demo.php file. The code is as follows:

<?php
    require_once &#39;./demo.php&#39;;
    require_once &#39;./demo.php&#39;;
    require_once &#39;./demo.php&#39;;
?>
Copy after login

The running results are as follows:

php中文网
https://www.php.cn/
Copy after login
Copy after login

Recommended learning: "PHP Video tutorial

The above is the detailed content of Does php have a file reference method?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!