Home >Backend Development >PHP Tutorial >Detailed explanation of the usage differences between include and require in PHP

Detailed explanation of the usage differences between include and require in PHP

WBOY
WBOYOriginal
2016-07-25 08:57:291104browse
  1. if($something){
  2. include("somefile");
  3. }
Copy the code

Example 2, but no matter what the value of $something is, the file somefile will be included in the file:

  1. if($something){
  2. require("somefile");
  3. }
Copy code

Example 3 fully illustrates the difference between these two functions.

  1. $i = 1;
  2. while ($i < 3) {
  3. require("somefile.$i");
  4. $i++;
  5. }
Copy the code

in this code , the program will include the same file each time it loops. From the code, you can see that this code hopes to include different files each time it loops. If you want to complete this function, you must turn to the function include():

  1. $i = 1;
  2. while ($i < 3) {
  3. include("somefile.$i");
  4. $i++;
  5. }
Copy code

2, when executing Different error reporting methods

The difference between include and require: When include introduces a file, if it encounters an error, it will give a prompt and continue to run the code below. When require introduces a file, if it encounters an error, it will give a prompt and stop running the code below. example: Write two php files named test1.php and test2.php. Note that there should not be a file named test3.php in the same directory. test1.php

  1. include ("test3.php");
  2. echo "abc";
  3. ?>
Copy code

test2.php

  1. require ("test3.php")
  2. echo "abc";
  3. ?>
Copy code

Code description:

Browse the first file. Because the test999.php file was not found, an error message appeared. At the same time, abc was displayed below the error message. What you saw may be similar to the following: Warning: include(test3.php) [function.include]: failed to open stream: No such file or directory in D:WebSitetest.php on line 2

Warning: include() [function.include]: Failed opening ‘test3.php’ for inclusion (include_path=’.;C:php5pear’) in D:WebSitetest.php on line 2 abc (the following is executed)

Browse the second file. Because the test3.php file was not found, an error message appeared. However, abc was not displayed below the error message. What you saw may be similar to the following: Warning: require(test3.php) [function.require]: failed to open stream: No such file or directory in D:WebSitetest2.php on line 2

Fatal error: require() [function.require]: Failed opening required 'test3.php' (include_path='.;C:php5pear') in D:WebSitetest.php on line 2

The following is not executed and ends directly.

In short, what is called during include execution is a process behavior and conditional, while require is a preset behavior and unconditional.



Statement:
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