Learn the difference between include_once and require_once

  • 2020-06-12 08:45:56
  • OfStack

Function and usage

You can reduce code duplication

include(_once) (" Path to file ") and require(_once) (" Path to File ")

(2) to understand

Basically, replace the include(_once),require(_once) line with the contents of the included file

(3) pay attention to

include/require included files must be added < ?php ? > Because when you include, the first thing you understand is that the contents of the file are ordinary strings that you encounter < ?php ? > When it comes to labels, explain

(4) path

You can use the absolute path or you can use the relative path; You can use forward and backward slashes for windows, but only forward slashes for linux, so you'd better use forward slashes

(5) the difference between

include is included. When a file is not found, an error from warning is reported and the program continues

require is required. If the file cannot be found, fatal error (fatal error) will be reported and the program will stop running

After adding once, the system will make a judgment. If it has been included, it will not include the second time

eg: There is 1 ES51en. php file which is < ?php $a++ ;? >

In the b. php file, $a=5; require_once (" ES63en.php "); echo $a; require_once (" ES69en.php "); echo $a;

The e result at the first output is 6, the output at the second is 6, that means. _once contains only one time, if once is not added, the output at the second point will be 7

6. Choose

For example, the system configuration, the lack of, the website does not let run, natural use require, if it is a segment of statistical procedures, less, the website is just a small number of statistics, not a must, you can use include

once is the difference in efficiency, plus once, although the system helps you to consider only loading once, but the judgment of the system is to reduce efficiency, therefore, should be at the beginning of development, the directory structure is adjusted well, try not to use _once.

Special usage

Use include/require to return the return value of the contained page

a.php page:... return $value; php :$v = include(" a.php ");
< !--[endif]-- >

This usage comes up every once in a while when doing web site configuration!

Related articles: