PHP中include和require的区别详解
最近有小伙伴,提问了有关于require_once的有关问题,对于程序中遇到的问题,无聊难度大小,只要bug存在就还值得我们去探索,去解决。针对这个问题,我把include和require的详细用法进行归纳。(结合查阅资料和自己的理解,整合出来的)希望能给有需要的小伙伴一点帮助。
include和require:
require()语句的性能与include()相类似,都是包括并运行指定文件。不同之处在于:对include()语句来说,在执行文件时每次都要进行读取和评估;而对于require()来说,文件只处理一次(实际上,文件内容替换require()语句)。这就意味着如果可能执行多次的代码,则使用require()效率比较高。另外一方面,如果每次执行代码时是读取不同的文件,或者有通过一组文件迭代的循环,就使用include()语句。
require的使用方法如:require("myfile.php"),这个语句通常放在PHP脚本程序的最前面。PHP程序在执行前,就会先读入require()语句所引入的文件,使它变成PHP脚本文件的一部分。include使用方法和require一样如:include("myfile.php"),而这个语句一般是放在流程控制的处理区段中。PHP脚本文件在读到include()语句时,才将它包含的文件读取进来。这种方式,可以把程式执行时的流程简单化。
•incluce在用到时加载
•require在一开始就加载
•_once后缀表示已加载的不加载
PHP系统在加载PHP程序时有一个伪编译过程,可使程序运行速度加快。但incluce的文档仍为解释执行。include的文件中出错了,主程序继续往下执行,require的文件出错了,主程序也停了,所以包含的文件出错对系统影响不大的话(如界面文件)就用include,否则用require。 require()和include()语句是语言结构,不是真正的函数,可以像php中其他的语言结构一样,例如echo()可以使用echo("ab")形式,也可以使用echo "abc"形式输出字符串abc。require()和include()语句也可以不加圆括号而直接加参数。
include_once()和require_once():
include_once()和require_once()语句也是在脚本执行期间包括运行指定文件。此行为和include()语句及require()类似,使用方法也一样。唯一区别是如果该文件中的代码已经被包括了,则不会再次包括。这两个语句应该用于在脚本执行期间,同一个文件有可能被包括超过一次的情况下,确保它只被包括一次,以避免函数重定义以及变量重新赋值等问题。
文件引用方式:
include()执行时需要引用的文件每次都要进行读取和评估,require()执行时需要引用的文件只处理一次(实际上执行时需要引用的文件内容替换了require()语句)可以看出若有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高,若每次执行代码时相读取不同的文件或者有通过一组文件叠代的循环,就使用include(),可以给想要包括的文件名设置变量,当参数为 include()时使用这个变量。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.
