Include and require in php

Source: Internet
Author: User
Include and require in php are accidentally seen today.
In PHP, the include () and require () functions are the same, but there are some differences in usage. include () is a conditional inclusion function, while require () it is an unconditional include function. For example, in the following example, if the variable $ somgthing is true, it will contain the file somefile:

If ($ something ){
Include ("somefile ");
}


However, no matter what value $ something takes, the following code will include the file somefile into the file:

If ($ something ){
Require ("somefile ");
}


The following interesting example fully demonstrates the differences between the two functions.

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


In this code, each loop contains the same file. Obviously, this is not the original intention of the programmer. from the code, we can see that this code will include different files in each loop. To complete this function, you must turn to the include () function ():

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

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.