Differences between include and require in PHP-php Tutorial

Source: Internet
Author: User
Differences between include and require in PHP

  1. If ($ something ){
  2. Include ("somefile ");
  3. }

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

  1. If ($ something ){
  2. Require ("somefile ");
  3. }

Example 3 illustrates the differences between the two functions.

  1. $ I = 1;
  2. While ($ I <3 ){
  3. Require ("somefile. $ I ");
  4. $ I ++;
  5. }

In this code, each loop contains the same file. From the code, we can see that this code requires different files to be included in each loop. To complete this function, you must turn to the include () function ():

  1. $ I = 1;
  2. While ($ I <3 ){
  3. Include ("somefile. $ I ");
  4. $ I ++;
  5. }

2. errors are reported in different ways.

Difference Between include and require: if an error occurs when the include file is introduced, a prompt is displayed and the following code is continued. If an error occurs when the require file is introduced, a prompt is displayed, and stop running the following code. Example: write two php files named test1.php and test2.php. Note that there is no file named test3.php in the same directory. Test1.php

  1. Include ("test3.php ");
  2. Echo "abc ";
  3. ?>

Test2.php

  1. Require ("test3.php ")
  2. Echo "abc ";
  3. ?>

Code description:

View the first file. because the test999.php file is not found, an error message is displayed. at the same time, abc is displayed under the error message. you may see a situation similar to the following: Warning: include (test3.php) [function. include]: failed to open stream: No such file or directory in D: \ WebSite \ test. php on line 2

Warning: include () [function. include]: Failed opening 'test3. php 'for declaration (include_path = '.; c: \ php5 \ pear ') in D: \ WebSite \ test. php on line 2abc (the following code is executed)

Browsing the second file, because the test3.php file is not found, an error message is displayed. However, abc is not displayed at the bottom of the error message. you may see a situation similar to the following: Warning: require (test3.php) [function. require]: failed to open stream: No such file or directory in D: \ WebSite \ test2.php on line 2

Fatal error: require () [function. require]: Failed opening required' test3. php '(include_path = '.; c: \ php5 \ pear ') in D: \ WebSite \ test. php on line 2

The following is not executed, and it ends directly.

In short, the calling during include is a process behavior with conditions, while require is a preset behavior with no conditions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.