PHP development basic tutorial files include

1. Overview of file inclusion

In actual development, it is often necessary to put the common code in the program into a file, and use the files of these codes Just include this file. This method helps improve the reusability of code and brings great convenience to code writing and maintenance. In PHP, there are four methods: require, require_once, include, and include-once to include a file.

Let’s compare their differences:

33.png


##Note:1, use less_ Once brings once because it will consume more resources to do detection work.

2. Introduction to the functions of the four methods

Note: It is recommended that students copy the code locally Test

Create a 1.php file and write two functions in it:

In the same directory as the above php file, I will create another 2 The .php file includes the 1.php file. In this way, my functions can be placed specifically in 1.php. When these functions are needed, I will include them from there:

I have learned the function of include through the above example. Next, we compare include and require:

In the code, we first use include to include the non-existent test.php file,

The local output result:

34.png

Use require to include the non-existent test.php file:

35.png

By comparing the above example, we found:

  • If the test.php file does not exist include will issue a warning and continue to execute the demo() and test() functions.

  • And require directly reports an error, and the demo() and test() functions cannot continue to execute.

We know from the table: The difference between include and include_once is to detect whether it is included repeatedly. If include_once is included repeatedly, the corresponding file will no longer be included, while include does not care about this. Whether the file has been imported or not, import it again.

Let’s try the same 2.php just now. We include functions.php twice, using include and include_once respectively:

Result:

36.png

Change to include_once and try again:

Output:

37.png

The prompt in the above picture is that the function demo cannot be redefined ().

As we mentioned in the function definition chapter, a function cannot be defined twice, otherwise an error will be reported. Because we included 3_1.php twice, it was executed twice, so this error was reported.

The reason why include_once does not report an error is because it detects that functions.php has been included before and no longer includes it.

As for the functions of require and require_once, can you use your smartest little brain to deduce them? require_once has two characteristics:

1. The included file must exist, otherwise execution will stop

2. Duplicate inclusion checks will be done

New learning:

  • The difference between Include and require. include encounters an error warning, but continues to execute. Require will alarm after encountering an error and will not execute further

  • The difference between inlcude and include_once is to detect whether it is included repeatedly. If include_once is included repeatedly, the corresponding file will no longer be included, while include does not care about this.


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!