Difference PHP function require and require_once and include_once and include the

include: the specified file is read and executed inside the program;

require: the contents of the target file will be read, and put themselves into these read-generation content;

include_once: includes and evaluates the specified file during script execution. This behavior and include statements similar, the only difference being that if the file has already been included too, will not be included again. As the name implies, it contains only once;

require_once: statements and require exactly the same, the only difference is that PHP checks whether the file has been included too, if it will not be included again.

1 include and require the difference

include and require in addition to the different ways of processing import documents, the biggest difference is: include a warning is generated and the script will continue to execute when introducing file does not exist, but require will result in a fatal error and the script execution stops.

<?php
  include 'no.php';
  echo '123'; ?>

If the file does not exist no.php, echo '123' can continue to perform sentence.

You may see a situation similar to the following:

click to see the work

<?php
require 'no.php';
echo '123'; ?>

If the file does not exist no.php, echo '123' sentence is not performed, when the require stopped.

You may see a situation similar to the following:

click to see the work

include the same as () and require () function, but there are some differences in usage, include () is a function of conditions comprising, but require () function is unconditionally comprising.

For example, in an example below, if the variable $ somgthing is true, then the file somefile comprising:

if($something){
include("somefile");
}

But no matter what value $ something, the following code will be included into the file somefile file:

if($something){
require("somefile");
}

Include_once and 2 include the difference (the difference between require and require_once)

include_once (require_once) statement includes and evaluates the specified file during script execution. This behavior is similar and include (require) statement, except that if the code from a file has already been included, it will not be included again, it will only contain one. include_once (require_once) need to query the list of files once loaded, confirm the existence of, and then loaded.

<?php
require '1.php';
require '1.php'; ?>

In this case 1.php is included twice.

<?php
require '1.php';
require_once '1.php'; ?>

In this case, the second time included, will not be included.

include (require) and include_once (require_once) the issue of choice

In general: include_once and require_once fall on the performance there will be, because he needs to contain over before judgment. Under normal circumstances, it is not required to consider, unless this has affected the performance of your program.

If you are using APC try not to use include_once, because apc.include_once_override configuration items are not well implemented (details Baidu).

Note 1: Alternative PHP Cache (APC) is an effective open for PHP cache storage means, it is possible to cache the intermediate code opcode of php.

require commonly used method, this function is usually placed on top of the PHP program, PHP program before execution, it will first read the introduction of the specified file require that it becomes a part of PHP pages. Commonly used functions, this method can also introduce it into the page.

include commonly used methods, this function is typically part of the process in process control. PHP program pages when reading include file, it will only read in. In this way, when the flow of program execution can be simplified.

In return, when coupled with low returns a reference in brackets not only efficiency but will also cause logic errors. So these functions do?

In theory: include and require plus without brackets behind no difference to the results, but with low efficiency parentheses, brackets so the back can not be without parentheses.

Guess you like

Origin www.cnblogs.com/liyong2019/p/11463086.html