search
HomeBackend DevelopmentPHP Tutorial古语题了,不得不再问一下include和require

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

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

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

我暂时这么想,因为也不懂什么编译之类的。请指点
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';

--------------------------------------------------------------------------------------------------------
引用
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 />}

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

<?php<br />echo 'test2';

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

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

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

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

报错如下:
引用
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
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
What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Explain type hinting in PHPExplain type hinting in PHPApr 28, 2025 pm 04:52 PM

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

What is PDO in PHP?What is PDO in PHP?Apr 28, 2025 pm 04:51 PM

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

How to create API in PHP?How to create API in PHP?Apr 28, 2025 pm 04:50 PM

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor