search
HomeBackend DevelopmentPHP TutorialThe difference between include require include_once require_once in PHP

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: php include '1.php';require'1.php';include_once '1.php'; require_once '1.php'; 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 difference1. 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
vue3+vite:src使用require动态导入图片报错怎么解决vue3+vite:src使用require动态导入图片报错怎么解决May 21, 2023 pm 03:16 PM

vue3+vite:src使用require动态导入图片报错和解决方法vue3+vite动态的导入多张图片vue3如果使用的是typescript开发,就会出现require引入图片报错,requireisnotdefined不能像使用vue2这样imgUrl:require(’…/assets/test.png’)导入,是因为typescript不支持require所以用import导入,下面介绍如何解决:使用awaitimport

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

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

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)