search
Homephp教程php手册php require(), include(), require

php中的 require(), include(), require_once(), include_once()是php中的一种语言特性,不是函数。 require() 这个语句通常放在PHP程序的最前面,PHP程序在执行前,就会预先载入 require() 所指定的文件,使之变成 PHP 程序的一部份,里面的函数,变量立马

php中的 require(), include(), require_once(), include_once()是php中的一种语言特性,不是函数。

 

require()
这个语句通常放在PHP程序的最前面,PHP程序在执行前,就会预先载入 require() 所指定的文件,使之变成 PHP 程序的一部份,里面的函数,变量立马生效,随时可以使用。常用的函数,参数设置可以用这个方法将它引入网页中。
注意:require() 语句不管放在php程序的任何地方,效果都是一样的,require()遇到错误,停止网页执行。

 

include()
这个语句一般放在流程控制中的处理部分里,PHP 程序在读到 include() 的文件时,才将它包含进来。所包含文件里的函数和变量在此刻生效,这种方式,可以更清晰的把握程序执行时的流程。
注意:所包含的代码继承了include 所在行的变量范围,比如:include() 出现于调用文件中的一个函数里,则被调用的文件中所包含的所有代码将表现得如同它们是在该函数内部定义的一样。所以它将遵循该函数的变量范围。遇到错误,跳过该行继续执行。

 

require_once()
和 require() 语句完全相同,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含,可以有效地避免把同一段代码包含进去而出现函数或变量重复定义的错误。虽然 require_once() 可以杜绝重复包含,但相应的牺牲了许多性能,执行效率没有 require() 高。

 

include_once()
和 require_once() 是一样的。

 

文章摘自 php require(), include(), require_once(), include_once() 的介绍和区别

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor