Home  >  Article  >  Backend Development  >  The difference between include require include_once require_once in PHP

The difference between include require include_once require_once in PHP

WBOY
WBOYOriginal
2016-08-08 09:21:24950browse
I am going to do some secondary development on a PHP open source program, and I would like to take this opportunity to systematically learn PHP. I once had a brief understanding of PHP, but because I had never used this language in my work, I gradually gave it up. It took me a long time to understand that project-driven learning is the best way. Only when you need to use a language, learning it will have better results, and it will not be easy to forget. Before secondary development, you must first understand the entire structure of the original program. When looking at the source code, I found that there are a large number of introduction statements. I remember that I have been very vague about the difference between include and require in PHP before, so I can no longer use it like this. attitude towards learning, so I stopped to understand the connection and difference between PHP, include and require. First of all, include and require both introduce the specified file. _once means that it is only introduced once, that is, what has been introduced before will not be introduced again. For example, there is a simple print echo '1
' in 1.php. The result of running the following program: <span><?</span><span>php include </span><span>'1.php'</span><span>;</span><span></span><span>require</span><span></span><span>'1.php'</span><span>;</span><span>include_once </span><span>'1.php'</span><span>;</span><span> require_once </span><span>'1.php'</span><span>;</span> will be

1

1

instead of

1

1

1

1

If the statement introduced by _once is placed above include and require, the result will be

1

1

1

1

include and require The difference

1. Loading failure is handled differentlyIn addition to the different ways of handling imported files, the biggest difference between include and require is:

include generates a warning when introducing a non-existing file and the script will continue to execute. ,

require will cause a fatal error and the script will stop executing.

php include 'hello.php'; echo 'world';?> If hello.php does not exist, the echo ‘world’ sentence can continue to be executed. php require'hello.php'; echo 'world';?> If hello.php does not exist, the echo ‘hello’ sentence will not be executed and will stop when require is reached. 2、include()是有条件包含函数,而 require()则是无条件包含函数。if(FALSE){ include 'file.php';//file.php不会被引入 }if(FALSE){require'file.php';//file.php将会被引入 3、文件引用方式include有返回值,而require没有$retVal = include(’somefile.php’);if(!empty($retVal)){ echo “文件包含成功”;}else{ echo “文件包含失败”;}include()执行时需要引用的文件每次都要进行读取和评估,
require()执行时需要引用的文件只处理一次(实际上执行时需要引用的文件内容替换了require()语句) 可以看出若有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高,
若每次执行代码时相读取不同的文件或者有通过一组文件叠代的循环,就使用include(),require通常使用方法,这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以这个方法将它引入网页中。include通常使用方法,这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化另外关于include和require后面是否加括号的问题,理论上来说:include和require后面加不加括号对执行结果没有区别,但是加上括号效率较低,所以后面能不加括号就不加括号。
转自:http://liuzhichao.com/p/1743.html

以上就介绍了PHP中include require include_once require_once 的区别,包括了方面的内容,希望对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
Previous article:PDO study notesNext article:PDO study notes