Home Backend Development PHP Tutorial 古语题了,不得不再问一下include和require

古语题了,不得不再问一下include和require

Jun 13, 2016 pm 12:06 PM
include nbsp php require

老话题了,不得不再问一下include和require
手册上,include和require的区别只有一点,就是出错时产生的不同级别的错误。
但是,网上关于两者的区别远不止这一点,搞得我非常混乱
下面这些是我从网上摘抄的一些观点和疑问
--------------------------------------------------------------------------------------------------------

引用
使用require语句调用文件时,只要程序一执行,就会立刻调用外部文件;而通过include语句调用外部文件时,只有程序执行到该语句时,才会调用外部文件。

什么是“只要程序一执行就调用”?
<?php<br />//aa.php<br />echo 'aa';<br />require './bb.php';
Copy after login

我暂时这么想,因为也不懂什么编译之类的。请指点
require情况:执行aa.php时,把aa.php和bb.php一起编译,然后执行;
include情况:先把aa.php编译->执行,发现有include,然后把bb.php编译->执行bb.php,完成后返回继续执行aa.php下面的代码。
--------------------------------------------------------------------------------------------------------
引用
require()所包含的文件中不能包含控制结构,而且不能使用return这样的语句。在require()所包含的文件中使用return语句会产生处理错误

这一点就非常搞不明白了。aa.php还是上面的那个代码,下面是bb.php,执行得好好的
<?php<br />//bb.php<br />if(true){<br />	echo 'bb';<br />}<br />return 'abc';
Copy after login

--------------------------------------------------------------------------------------------------------
引用
require()不能在循环体中根据条件的不同而包含不同的文件。require()语句只会在第一次执行时调用它所包含的文件中的内容替换本身这条语句,当再次被执行时只能执行第一次所包含的语句。但是include()语句可以在循环体中来包含不同的文件。

文章中举了个例子并得出结论:
引用
可以从以上这段代码中看出,每一次循环的时候,程序都将把同一个文件包含进去,很显然这不是我们想要的,可以看出这段代码希望在每次循环时,将不同的文件包含进来,如果要完成这个功能,只能使用函数include()

<?php<br />//aa.php<br />echo 'aa';<br />$i=1;<br />while($i<3){<br />	require './test' . $i . '.php';<br />	$i++;<br />}
Copy after login

但是,我测试却能将test1.php和test2.php都引入进来
<?php<br />echo 'test1';
Copy after login

<?php<br />echo 'test2';
Copy after login

输出:aatest1test2
--------------------------------------------------------------------------------------------------------------------------
引用
include()是有条件包含函数,而require()则是无条件包含函数。意思是使用require,在条件控制语句中,即便条件为假,也会引入进来。只不过是引入的文件不会起到任何可见的作用,只会加重程序负担。

<?php<br />//aa.php<br />if(false){<br />	require './bb.php';<br />}
Copy after login

是否是这样?如何能证明已经引入了bb.php?
------------------------------------------------------------------------------------------------------------------------
另外,还有一个关于报错的问题,手册上这么讲

<?php<br />//aa.php<br />include 'nofile.php';<br />//require 'nofile.php';
Copy after login

报错如下:
引用
Warning: include(nofile.php) [function.include]: failed to open stream: No such file or directory in D:\www\Apache2.2\htdocs\02PHP\index.php on line 3

//include时
Warning: include() [function.include]: Failed opening 'nofile.php' for inclusion (include_path='.;C:\php\pear') in D:\www\Apache2.2\htdocs\02PHP\index.php on line 3

//require时
Fatal error: require() [function.require]: Failed opening required 'nofile.php' (include_path='.;C:\php\pear') in D:\www\Apache2.2\htdocs\02PHP\index.php on line 3

第一个warning是报什么样的错误?两个都是warning
第二个看像是在include_path里找不到而报的错?
------解决思路----------------------
所有的说明一最新的手册为准

网上关于include和require的描述,大多是基于php4的
到了php5时代,就只剩下 出错时产生的不同级别的错误 的区别了
------解决思路----------------------
IT知识的更新换代太快了,一切以官方手册为准,也可以研读一下PHP源码,看看是怎么处理的,那是最正确的。
Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles