


What is the difference between include and require in PHP? What is the difference between Include_once?
The previous article introduced to you "What are the commonly used system functions for files in PHP? What is the principle of loading files? 》, this article continues to introduce to you what is the difference between include and require in PHP? What is the difference between Include_once? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
●The difference between Include and require.
First understand the difference between Include and include_once:
Include system will encounter it once and execute it once; if the same file is loaded multiple times , then the system will execute it multiple times;
Include_once:
If the system encounters it multiple times, it will only execute it once
Specifically, we use the code as Example:
Create two files named include1 and include2 respectively; the code is as follows:
include1:
<?php //被包含的文件 //定义数据 $a = 1; define('PI',3.14); ?>
include2:
<?php //包含文件:使用数据 //包含文件 include 'include1.php'; //包含当前文件include2. php所在文件夹下的include1. php echo $a,PI; //再次加重 include 'include1.php'; ?>
Code demonstration results As follows:
When we comment out include 'include1.php'; based on the original code, then we add
include_once 'include1.php'; The running result will show that notices and errors are no longer reported, but we need to pay attention to the fact that the same constant may have repeated definitions, that is, if the inclusion is successful, it will be repeated. Defined. At this time, there is no error in the result of our second run, because the system finds that we have defined it once in include1. We can know that the system will automatically query, so we also know that Include_once:
The system encountered Multiple times, it will only be executed once
, this is the most obvious difference between Include and include_once
The difference between Require and include: The essence is that they both include files, the only difference is that include When the file is not found, the error form is different.
Specifically, let’s take the code as an example:
We first define a file randomly, and the results show two errors. First, it says that the system cannot open the file we defined. Second, the system cannot find it. to the file, but the system reports a warning, which does not affect our execution. When we put the code normally below the code, we will find that it will be parsed normally.
<?php //require和include的区别 //include包含文件 include 'b.php'; ?>
The code demonstration results are as follows:
<?php //require和include的区别 //include包含文件 include 'a.php'; echo '我喜欢你'; ?>
The code demonstration results are as follows:
As can be seen from the above, although we will report an error above, it will not affect our execution results. ;
The error level of Include is relatively mild: it will not prevent code execution.
Requires higher requirements: if it contains an error code, it will not be executed (the code after require)
Specifically, we use the code as Example:
<?php //require和include的区别 //include包含文件 //include 'a.php'; //requre 包含文件 require 'b.php'; echo '我喜欢你'; ?>
The code demonstration result is as follows:
The one difference from the code we run above is that require will report a fatal error: not found Corresponding file, and the code we entered will no longer be executed
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between include and require in PHP? What is the difference between Include_once?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

When we write web pages using PHP, sometimes we need to include code from other PHP files in the current PHP file. At this time, you can use the include or include_once function to implement file inclusion. So, what is the difference between include and include_once?

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages or frameworks, require is used to perform specific tasks or functions.

Detailed explanation of the role and usage of the require keyword in PHP In PHP development, require is a very commonly used keyword. Its function is to include the specified file for use by the current script. This article will explain in detail the function and use of the require keyword. 1. The role of the require keyword The require keyword can include the contents of a file into the current script. It is usually used to include some necessary external files, such as library files, configuration files, etc. Use req

Steps to resolve fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear') in PHP header When developing websites or applications using PHP, we often encounter various errors . One of the common errors is "fatalerror:require():Failed

Steps to resolve FatalError:require():Failedopeningrequired'data/tdk.php' in PHP header When developing and maintaining PHP websites, we often encounter various errors and exceptions. One of the common errors is "FatalError:require():Failedopeningrequired'data/tdk.php'".

Introduction In PHP, various URL style protocols can be used in conjunction with file system functions, with the help of corresponding built-in wrappers. Custom wrappers can also be defined using the stream_wrapper_register() function. The default wrapper in PHP is file://, which represents the local file system. If no other protocol is explicitly used, the PHP parser will treat it as a filesystem wrapper. The file name parameters passed to the file system functions fopen(), file_get_contents(), etc. use the file:// protocol by default. When the file name does not begin with a forward slash, a backslash, or the driver in Windows

Unable to include .ini file in main php,ini file. In contrast, when compiling PHP, the line --with-config-file-scan-dir=PATH&a
