Home > Backend Development > PHP Tutorial > What is the difference between include() and require() functions in PHP?

What is the difference between include() and require() functions in PHP?

青灯夜游
Release: 2023-04-05 14:30:01
Original
4726 people have browsed it

The include() and require() functions in PHP perform the same function and can help us call files. So what's the difference between them? The following article will help you understand the difference between include() and require() functions. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]

What is the difference between include() and require() functions in PHP?

##PHP include() function# The ##include() function is used to copy all the contents (text) within the file called within the function into the file calling it; this occurs before the server moves beyond the code.

Example: Use the include() function to call a file named demo.php

demo.php file:

<?php 
// 要包含的文件
echo "Hello PHP中文网!";
?>
Copy after login

Now let us try to include this file into another php file (index.php). We will see the contents of the file displayed.

index.php file:

<?php  
header("content-type:text/html;charset=utf-8");
include("demo.php"); 
echo "<br>包含上述文件"
?>
Copy after login

Output:

What is the difference between include() and require() functions in PHP?

##PHP require() function

The require() function performs the same operation as the include() function. It also fetches the required files and copies the entire code into the file where the require() function is called.

Example: Using the require() function to call a file named demo.php

demo.php file

<?php 
// 要包含的文件
echo "PHP中文网!";
?>
Copy after login

Now, if we try to use the require() function to call this The file is included into the web page.

index.php file:

<?php  
header("content-type:text/html;charset=utf-8");
require("demo.php");
echo "<br>包含上述文件";
?>
Copy after login
Output:

What is the difference between include() and require() functions in PHP?##include() and require() functions The difference between

include() and require() functions have the same effect and produce the same results; but there is a difference between them. 1. When the called file is missing

●include() will display a warning (E_WARNING), but it will not stop script execution.

If we don't have a file named demo.php, then in case of include(), the following output will be shown with a warning about the missing file, but at least the output from the index.php file will be shown:

● require() will throw a fatal error (E_COMPILE_ERROR) because it cannot find the specific file at the specified location and therefore stops execution.

What is the difference between include() and require() functions in PHP?For the require() function, if the file contained in it (example: demo.php) is missing, a fatal error will occur, no output will be displayed, and execution will stop.

This is the main difference between include() and require() functions; but it also shows that require() function is better than include() function because if the file is missing or generated With this error, the script should not continue executing.

What is the difference between include() and require() functions in PHP?2. Loop or control structure

include() function can be used for loops or control structures. However, the require() function cannot be used with loops or control structures.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the difference between include() and require() functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template