require,include,require_once和include_once区别
1.require() statement
require() statement is used to specify the file instead of the statement itself, just like the include() statement in C language. If the URL fopen wrappers in the php configuration file php.ini is turned on (it is turned on by default), you can use the URL to specify the location of the file to achieve remote file calling.
One thing is to pay special attention when using require() and include() statements. That is, in the included file, the processor interprets the content according to the HTML mode, and returns to the PHP mode after processing the included content. So if you need to use PHP syntax in the included file, you must use the correct PHP start and end tags to include these statements.
require() and include() are a language feature in PHP, not a function. They are different from functions in many ways.
For example: the file included in require() cannot contain control structures, and statements such as return cannot be used. Using the return statement in a file included with require() will generate a processing error.
Unlike the include() statement, the require() statement will unconditionally read the contents of the file it contains, regardless of whether these statements are executed. So if you want to include different files according to different conditions, you must use the include() statement. Of course, if the statement at the location of require() is not executed, the statements in the file contained by require() will not be executed either.
require() cannot include different files based on different conditions in the loop body. The require() statement will only call the content of the file it contains when it is executed for the first time to replace the statement itself. When it is executed again, only the statement included in the first time will be executed. But the include() statement can include different files in the loop body.
The variables in the require() statement inherit the variable scope where the require() statement is located. All variables accessible at the location of the require() statement are accessible in the file included in the require() statement. If the require() statement is located inside a function, then the statements in the included file are equivalent to being defined inside the function. The
require() statement will read the file referenced by require before the PHP program is executed, so require is usually placed at the beginning of the program. Therefore, special attention should be paid to the fact that the require statement is a bit strong. Regardless of whether the program really needs the referenced files, as long as you use the require statement, it will include them! Even if you use this function to include in a conditional control statement, even if the condition is not true, the referenced file will be included! Zombies are formed. These zombies will not have any visible effect during operation, but it will obviously increase the burden, so pay special attention to this! If an inclusion error occurs using the require statement, the program will output an error message and stop running! !
If the require() statement includes a remote file by declaring the URL of the file, and the remote server interprets the file according to the PHP code, the content contained in the local PHP file is the result of processing on the remote server. For example:
Copy the code The code is as follows:
/*
This example assumes that some_server server can interpret .php files but not .txt files. In the remote file
need variables $varfirst and $varsecond
*/
/*cannot be executed correctly, the remote server does not process .txt files*/
require("http://some_server/file.txt?varfirst=1&varsec>
/*Incorrect, you can only find the file.php file on the local machine*/
require("file.php?varfirst=1&varsec>
/*Correct statement*/
require("http://some_server/file. php?varfirst=1&varsec>
$varfirst=1;
$varsec>require("file.txt"); /*Correct statement*/
require("file.php"); /*Correct statement*/
Originally in php3.0, files included in require() can use the return statement, but the condition is that the return statement cannot appear inside {}, but must appear in the global scope of the included file in php4.0. This function of require() has been cancelled, but it can still be implemented using include().
2.include() statement
The include() statement has many similarities with the require() statement. Except for the parts of the require() statement above that are not explicitly stated to be applicable to include(), the functions of the require() statement are fully applicable to the include() statement. The functions and features of the include() statement that are not included in the require() statement are introduced below. .
The include statement will only read the file to be included when it is executed. For error handling convenience, use the include statement. If an include error occurs, the program will skip the include statement. Although the error message will be displayed, the program will still Continue executing!
The PHP processor will reprocess it every time it encounters an include() statement, so you can use include() in conditional control statements and loop statements to include different statements according to different situations. File.
For example:
Copy the codeThe code is as follows:
$files=array('first.php','second.php','third.php');
for($i=0;$i
include $files[$i];
}
?>
The return statement can be used to return a value in the files included in the include() statement in php3.0 and php4.0. And stop executing the content below the included file. But php3.0 and php4.0 handle such situations differently. In php3.0 the return statement cannot be contained within {} unless it is in a function, because then it represents the return value of the function rather than the return value of the file. In php4.0, there is no such restriction. Users can even return a number in the file, just like the return value of a function. Such statements usually report errors in
php3.0. The following is an example:
Assume that the included file is test.inc and the main file main.php is located in a directory. The content of test.inc is as follows:
test.inc
Copy the code The code is as follows:
echo "Before the return
n";
if(1)
{
return 27;
}
echo "After the return
n";
?>
Suppose the main.php file contains the following statement:
Copy the codeThe code is as follows :
$retval=include('test.inc');
echo "File returned:'$retval'
n";
?>
php3.0 explanation The compiler will report an error on the second line and cannot get the return value of the include() statement. But in php4.0, you will get the following result:
Before the return
File returned: '27'
Let's assume that main.php is changed to:
Copy the codeThe code is as follows:
include('test.inc');
echo "Back in main.html
n";
?> The output result of
in php4.0 is:
Before the return
Back in main.html
The output in php5.0 is also:
Before the return
Back in main.html
The output in php3.0 The result is:
Before the return
27Back in main.html
Parse error:parse error in /apache/htdocs/phptest/main.html on line 5
The above error occurs because of the return statement Located inside {} and not inside a function. If {} is removed so that it is located in the outermost layer of test.inc, the output result is:
Before the return
27Back in main.html
The reason why 27 appears is because in php3.0 Include() return is not supported.
3.require_once() and include_once() statements
require_once() and include_once() statements correspond to require() and include() statements respectively. The require_once() and include_once() statements are mainly used when multiple files need to be included, which can effectively avoid errors in repeated definitions of functions or variables caused by including the same piece of code. For example: If you create two files util.inc and fool.inc, the program codes are:
util.inc:
Copy the code The code is as follows:
define(PHPVERSION ,floor(phpversion()));
echo "GLOBALS ARE NICE
n";
function goodTea()
{
return "Olong tea tastes good!";
}
?>
and fool .inc:
Copy the code The code is as follows:
require ("util.inc");
function showVar($var)
{
if(PHPVERSION==4)
{
print_r($var);
}
else
{
var_dump($var);
}
}
?>
Then include these two files in error_require.php:
Copy the codeThe code is as follows:
require("fool.inc");
require("util.inc");//This sentence will generate an error
$foo=array("1" ,array("complex","quaternion"));
echo "this is requiring util.inc again which is also
n";
echo "required in fool.incn";
echo "Running goodTea:". goodTea()."
n";
echo "Printing foo:
n";
showVar($foo);
?>
globals are nice
globals are nice
Fatal error: Cannot RedeClare Goodtea () in Util.Inc on Line 4 ) statement instead of the require() statement, the above error will not occur. We changed the require() statement in error_require.php and fool.inc to require_once() statement and renamed it to error_require_once.php. This is the result shown below:
GLOBALS ARE NICE
this is requiring util.inc again which is also
required in fool.inc Running goodTea:Olong tea tastes good!
Printing foo:
Array([0] => 1 [1] => Array ([0] => ; complex [1] = quaternion))
The syntax of the include_once() statement is similar to the include() statement. The main difference is to avoid repeated definitions of functions or variables caused by including a file multiple times.
The require_once statement has a reference chain, which ensures that the file is added to your program only once and avoids conflicts between variable values and function names.
Same as the require_once statement, the include_once statement extends the functionality of include. During program execution, the specified file is included. If the program referenced from the file has been included previously, include_once() will not include it again. That is to say, the same file can only be referenced once! The
include_once() statement includes and runs the specified file during script execution. This behavior is similar to the include() statement, the only difference is that if the code in the file is already included, it will not be included again. As the name of this statement implies, it will only be included once.
include_once() should be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.
For more examples of using require_once() and include_once(), see the PEAR code in the latest PHP source program distribution package.
The return value is the same as include(). If the file is included, this function returns TRUE.
Note: include_once() is newly added in PHP 4.0.1pl2.
Note: Be aware that the behavior of include_once() and require_once() in case-insensitive operating systems (such as Windows)
may not be expected.
Example: include_once() is case-insensitive under Windows
Copy the code
The code is as follows:
include_once("a.php"); // this will include a .php include_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)
?>
This behavior was changed in PHP 5, the path is specified first ization, so the implementation of C:PROGRA~1A.php and C:Program Filesa.php are the same, and the file will only be included once.
Under the win32 platform, they are included first and then executed, so it is best not to have include or require statements in the included files, which will cause directory confusion. Maybe the situation is different under Linux, I haven't tested it yet.
If a file does not want to be included multiple times, you can use include_once or require_once## to read and write document data.
Copy the code
The code is as follows:
function r($file_name) { $filenum=@fopen($file_name,"r");
@flock($filenum,LOCK_SH );$file_data=@fread($filenum,filesize($file_name));
@fclose($filenum);
return $file_data;
}
function w($file_name,$data,$method="w" ){
$filenum=@fopen($file_name,$method);
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$data);
fclose($filenum);
return $file_data;
}
The above has introduced the differences between require, include, require_once and include_once, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
