Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use include and require in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use include and require in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Summary

The performance of the require () statement is similar to that of include (), which includes and runs the specified file. The difference is that for the include () statement, the file is read and evaluated every time the file is executed, while for require (), the file is processed only once (in fact, the file content replaces the require () statement). This means that it is more efficient to use require () if it is possible to execute code multiple times. On the other hand, if you read a different file each time the code is executed, or if there is a loop that iterates through a set of files, use the include () statement.

The usage of require () is as follows:

Require ("myfile.php")

This statement is usually placed at the front of the PHP script. Before execution, the PHP program reads in the file introduced by the require () statement, making it part of the PHP script file.

Include () is used in the same way as require:

Include ("myfile.php")

This statement is typically placed in the processing section of the process control.

The PHP script file reads the files it contains only when it reads the include () statement. In this way, the process of running the program can be simplified.

Incluce loads when needed require loads the _ once suffix at the beginning to indicate that the loaded ones are not loaded

The PHP system has a pseudo-compilation process when loading the PHP program, which can make the program run faster. However, the documentation of incluce is still interpreted and executed. There is an error in the include file, the main program continues to execute, the require file goes wrong, and the main program stops, so if the included file error has little impact on the system (such as interface files), then use include, otherwise use require.

The require () and include () statements are language structures, not real functions, and can be like other language structures in php. For example, echo () can use the echo ("ab") form, or you can use the echo "abc" form to output the string abc. The require () and I nclude () statements can also directly add parameters without parentheses.

The include_once () and require_once () statements also include running the specified file during script execution. This behavior is similar to the include () statement and require (), and is used in the same way. The only difference is that if the code in the file is already included, it will not be included again. These two statements should be used to ensure that the same file is included only once during the execution of the script, to avoid problems such as function redefinition and variable re-assignment.

Explain the error report in detail

When include introduces the file, if it encounters an error, it will give a prompt and continue to run the following code.

When require introduces the file, if it encounters an error, it will give a prompt and stop running the following code.

To speak with an example, write two php files named test-include.php and test-require.php. Note that there is no file named test-nothing.php in the same directory.

Test-include.phptest-require.php

Browse http://localhost/test-include.php, because we can't find the test-nothing.php file, we see the error message. At the same time, the bottom of the error message shows abc. You may see something similar to the following:

Warning: include (test-nothing.php) [function.include]: failed to open stream: No such file or directory in D:\ www\ test-include.php on line 2Warning: include () [function.include]: Failed opening 'test-nothing.php' for inclusion (include_path='.;C:\ php5\ pear') in D:\ www\ test-include.php on line 2abc

Browse http://localhost/test-require.php, because we can't find the test-nothing.php file, we see the error message, but there is no abc at the bottom of the error message. You may see something similar to the following:

Warning: require (test-nothing.php) [function.require]: failed to open stream: No such file or directory in D:\ www\ test-require.php on line 2Fatal error: require () [function.require]: Failed opening required 'test-nothing' (include_path='.;C:\ php5\ pear') in D:\ www\ test-require.php on line 2 File reference method

The files that need to be referenced when include () is executed have to be read and evaluated every time, and the files that need to be referenced during require () execution are processed only once (in fact, the contents of the files that need to be referenced during execution replace the require () statement). It can be seen that if there is code containing one of these instructions and code that may be executed multiple times, it is more efficient to use require (). If you use include () if you read a different file each time the code is executed, or if you have a loop iterated through a set of files, you can set a variable for the file name you want to include, using this variable when the argument is include ().

This is the end of the content of "how to use include and require in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report