


What are the differences between several include file functions in PHP?
Difference: 1. If include encounters an error, it will give a prompt and continue execution; while require will terminate program execution. 2. If the included file in require_once and include_once has already been included, it will not be included again, but include and require will.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
File inclusion refers to the inclusion of another source file All contents are included in the current source file for use, which is also commonly referred to as introducing external files. Referencing external files can reduce code reusability and is an important skill in PHP programming.
PHP provides 4 very simple but useful include statements, namely include statement, require statement, include_once statement and require_once statement. There are certain differences in the use of these four statements. Each is explained in detail below.
1. include statement
When using the include statement to include an external file, the external file will only be included when the code is executed to the include statement. , when an error occurs in the included external file, the system will give a warning, and the entire PHP program will continue to execute.
The syntax format of the include statement is as follows:
include('filename') 或者 include 'filename'
where filename is the file path to be included (both relative and absolute paths are acceptable), filename is a string, so single quotes are required' ' or wrapped in double quotes " ". At the same time, the brackets after include can also be omitted. When the brackets are omitted, include needs to be separated from the following filename by a space.
For the convenience of demonstration, here we prepare a demo.php file and simply define a $str variable in it, as shown below:
<?php $str = 'https://www.php.cn/'; ?>
[Example] Use the include statement to include the demo .php file, the code is as follows:
<?php include './demo.php'; echo $str; ?>
The running result is as follows:
https://www.php.cn/
2. require statement
require The usage of the statement is similar to the include statement, both of which implement references to external files. Before the PHP file is executed, the PHP parser will replace the require statement with the entire content of the referenced file, then form a new PHP file with other statements except the require statement, and finally execute the program code according to the new PHP file.
Note: Because the require statement is equivalent to completely copying the contents of another source file into this file, it is generally placed at the beginning of the source file to reference the public function files and files that need to be used. Public class files, etc.
The require statement is almost exactly the same as the include statement. The difference is that when the included file does not exist or there is an error, the require statement will issue a Fatal error and terminate program execution, while the include statement will issue a Warining warning. But the program will continue to execute downwards.
The syntax format of the require statement is:
require(filename) 或者 require 'filename'
The parameter filename is the path of the file to be included, and its characteristics are the same as the parameters in the include statement.
[Example] Use the require statement to include the demo.php file defined above. The code is as follows:
<?php require './demo.php'; echo $str; ?>
The running results are as follows:
https://www.php.cn/
3. include_once statement
The include_once statement is similar to the include statement. The only difference is that if the included file has already been included, it will not be included again. include_once can ensure that the same file is only included once during script execution to avoid problems such as function redefinition and variable reassignment.
Let’s adjust the demo.php file defined above as follows:
<?php echo 'PHP中文网<br>'; echo 'https://www.php.cn/'; ?>
[Example] Use the include_once statement to include the demo.php file, the code is as follows:
<?php include_once './demo.php'; include_once './demo.php'; include_once './demo.php'; ?>
The running results are as follows:
PHP中文网 https://www.php.cn/
4. require_once statement
require_once statement is an extension of require statement, and its function is the same as require The statements are basically similar. The difference is that when applying the require_once statement, it will first check whether the file to be included has been included elsewhere in the program. If so, the file will not be included again.
[Example] Use the require_once statement to include the demo.php file. The code is as follows:
<?php require_once './demo.php'; require_once './demo.php'; require_once './demo.php'; ?>
The running results are as follows:
PHP中文网 https://www.php.cn/
Recommended learning: "PHP Video tutorial》
The above is the detailed content of What are the differences between several include file functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

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.

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

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