Which of the following is more reliable for require and include in php?

Source: Internet
Author: User
From the perspective of best practices, which of the following is the most widely used and more reliable? (Php beginners, Do not spray) from the perspective of best practices, which one is the most used and more reliable? (Php beginners, Do not spray)

Reply content:

From the perspective of best practices, which of the following is the most widely used and more reliable? (Php beginners, Do not spray)

The php script introduces require, and the front-end script uses include. Because require is more strict, a serious error will be reported if it is not introduced successfully, so that the program will stop, but the include will not. We recommend that you use require for program rigor.

The difference between require and include is that the processing method after the reference fails:

  • A fatal E_COMPILE_ERROR error occurs when require fails;

  • If the include operation fails, only the E_WARNING warning is generated;

It can be understood as follows:

  • Require: required (must );

  • Include: required (need );

@ Mgckid is a good practice. That is:

  • Code that involves rigorous business logic ("Php script 』) We recommend that you use require. If the reference fails and the execution continues, it may damage important data;

  • It is only used for Code related to the Fight page ("Front-End Script 』) You can use include. Even if the reference fails, subsequent operations will not damage the data. This makes the page missing modules or 4xx errors at most easier to accept than 5xx errors.

Check the specific logic. For example, the SQL statement used to operate the database is suitable for require. If the front-end html is introduced, it is suitable for include.

Require_once ("");

If it is a class library, the require references multiple times and reports an error. It is best to use require_once

In comparison, there is another include_once, which can be used when file accessibility is the best.

Incluce loaded when used
Require load at the beginning
* _ Once loaded does not load

include("file.php") include "file.php";

Require unconditional loading include conditional Loading

Use case by case, to see the importance of introducing files

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.