search
Homephp教程php手册[PHP]区分include(),require(),include_once(),require_once()

[PHP]区分include(),require(),include_once(),require_once()

Jun 13, 2016 am 10:55 AM
includeoncephprequireanddistinguishthe differenceof

1.include()与require()的区别(同理可区分include_once()与require_once())

 

include(),require()语句包含并运行指定文件。 这两种结构除了在如何处理失败之外完全一样。

 

include()产生一个警告而require()则导致一个致命错误。换句话说,如果想在遇到丢失文件时停止处理页面就用 require()。include()就不是这样,脚本会继续运行

例1:include()产生一个Warning而require()则导致一个Fatal error。

 

zhanhailiang@linux-06bq:~> php -r "include('a.php');"

 

Warning: include(a.php): failed to open stream: No such file or directory in Command line code on line 1

 

Warning: include(): Failed opening 'a.php' for inclusion (include_path='.:/usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php') in Command line code on line 1

zhanhailiang@linux-06bq:~> php -r "require('a.php');"

 

Warning: require(a.php): failed to open stream: No such file or directory in Command line code on line 1

 

Fatal error: require(): Failed opening required 'a.php' (include_path='.:/usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php') in Command line code on line 1

2.include()与include_once()的区别(同理可区别require()与require_once())

 

include_once()语句在脚本执行期间包含并运行指定文件。此行为和include()语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。

include_once()应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。 

返回值和include()相同。如果文件已被包含,本函数返回TRUE。

例1:include()会多次包含指定文件,而include_once()不会。

 

zhanhailiang@linux-06bq:~> cat a.php

echo '1'.PHP_EOL;

zhanhailiang@linux-06bq:~> php -r "include('a.php');include('a.php');"

1

1

zhanhailiang@linux-06bq:~> php -r "include_once('a.php');include_once('a.php');"

1 www.2cto.com

例2:include_once()避免函数重定义。

 

zhanhailiang@linux-06bq:~> cat a.php 

echo '1'.PHP_EOL;

 

function test() {}

zhanhailiang@linux-06bq:~> php -r "include('a.php');include('a.php');"

1

 

Fatal error: Cannot redeclare test() (previously declared in /home/zhanhailiang/a.php:4) in /home/zhanhailiang/a.php on line 4

zhanhailiang@linux-06bq:~> php -r "include_once('a.php');include_once('a.php');"

1

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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor