


What are the ways to import files in PHP? The PHP import file has four statements: include, require, include_once, require_once. Let’s take a look at specific examples of PHP import files.
Basic syntax
require: require function is generally placed at the front of the PHP script and will be executed before PHP First read in the imported file specified by require, include and try to execute the imported script file. The way require works is to improve the execution efficiency of PHP. After it is interpreted once in the same web page, it will not be interpreted the second time. But similarly, because it will not repeatedly interpret the imported file, you need to use include when using loops or conditional statements to introduce files in PHP.
include: can be placed anywhere in the PHP script, usually in the processing part of the process control. When the PHP script is executed to the file specified by include, it will be included and attempted to execute. This method can simplify the process of program execution. When encountering the same file for the second time, PHP will still re-interpret it again. The execution efficiency of include is much lower than that of require. At the same time, when the user-defined function is included in the imported file, PHP will have the problem of repeated function definition during the interpretation process. .
require_once/include_once: The functions are the same as require/include respectively. The difference is that when they are executed, they will first check whether the target content has been imported before. If it has been imported, Then the same content will not be reintroduced again.
The difference between each other
include and require:
include has Return value, while require has no return value
When include fails to load a file, it will generate a warning (E_WARNING), and the script will continue to execute after the error occurs. So include is used when you want to continue execution and output results to the user.
//test1.php <?php include './tsest.php'; echo 'this is test1'; ?> //test2.php <?php echo 'this is test2\n'; function test() { echo 'this is test\n'; } ?> //结果: this is test1
require will generate a fatal error (E_COMPILE_ERROR) when loading fails, and the script will stop executing after the error occurs. Generally used when subsequent code depends on the loaded file.
//test1.php <?php require './tsest.php'; echo 'this is test1'; ?> //test2.php <?php echo 'this is test2\n'; function test() { echo 'this is test\n'; } ?>
Result:
include and include_once:
The files loaded by include will not be judged as duplicates , as long as there is an include statement, it will be loaded once (even if repeated loading may occur). When include_once loads a file, there will be an internal judgment mechanism to determine whether the previous code has been loaded. What needs to be noted here is that include_once is judged based on whether a file with the same path has been previously imported, rather than based on the content of the file (that is, the content of the two files to be imported is the same, and using include_once will still introduce two).
//test1.php <?php include './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1this is test2 //test1.php <?php include './test2.php'; echo 'this is test1'; include_once './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1 //test1.php <?php include_once './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1this is test2 //test1.php <?php include_once './test2.php'; echo 'this is test1'; include_once './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1
require and require_once: The same difference as include and include_once.
Execution process when loading
1. Exit php script mode from the include (require) statement (enter html code mode)
2. Load the code in the file set by the include statement and try to execute it
3. Exit the html mode, re-enter the php script mode, and continue the execution of the subsequent script program
//test1.php <html> <body> 主文件开始位置: <?php echo "<br> 主文件中位置 A"; include "./test2.php"; //要载入的文件 echo "<br> 主文件中位置 B"; ?> <br> 主文件结束位置 </body> </html> //test2.php <br> 被载入文件位置 1 <?php echo "<br> 被载入文件位置 2"; ?> <br> 被载入文件位置 3
Result:
Analysis:
##Path problem during loading
Relative path:
Locate the location of a loaded file relative to the location of the current web page file../ 表示表示当前位置,即当前网页文件所在的目录 . . / 表示上一级位置,即当前网页文件所在目录的上一级目录 //例如: include "./test2.php"; require "../../test3.html";
Absolute path:
is divided into local absolute path and network absolute pathLocal absolute path:
Search recursively from the local root directory layer by layer until you find the file to be imported in the corresponding directory.include "C:/PHP/test/test2.php";We all know that absolute paths are not conducive to the portability and maintainability of the project, so it is generally rare to write absolute paths directly in the code, but what should we do if we need to use absolute paths? ? There are magic constants __DIR__ and global array $_SERVER in PHP. The usage is as follows:
<?php define('DS') or define('DS',DIRECTORY_SEPARATOR); echo "使用绝对路径引入(方法一)"; include __DIR__ . DS . 'test2.php'; echo "使用绝对路径载入方法(方法二)"; $root = $_SERVER['DOCUMENT_ROOT']; // 获得当前站点的根目录 include $root.DS.'node_test'.DS.'inAndRe'.DS. 'test2.php'; ?>
Absolute network path:
Link to the file through the URL, and the server will The file pointed to by the URL will be returned after executioninclude "http://www.lishnli/index.php"
No path:
Only the file name is given but no path information is given. At this time, PHP will be in the current web page directory. Search for the file. If a file with the same name is found, execute it and import it.需要注意:无论采用哪种路径,必须要加上文件后缀名,这四种文件载入方式不能识别无后缀的文件。
//test1.php include "./test2.php"; //结果:this is test2 //test1.php include "./test2"; //结果:
返回值的比较
上文说道include有返回值,而require无返回值
对于include,如果载入成功,有返回值,返回值为1;如果载入失败,则返回false.
对于require,如果载入成功,有返回值,返回值为1;如果载入失败,无返回值。
//test1.php <?php $a = include "./test2.php"; var_dump($a); echo "<br>"; $b = include "./test2.phps"; var_dump($b); echo "<br>"; $c = require "./test2.php"; var_dump($c); echo "<br>"; $d = require "./test2.phps"; var_dump($d); ?>
输出:
当文件中有return:
当被载入文件中有return语句时,会有另外的机制,此时return语句的作用是终止载入过程,即被载入文件中return语句的后续代码不再载入。return语句也可以用于被载入文件载入时返回一个数据。
//test1.php <?php $a = include "./test2.php"; echo "<br>"; var_dump($a); ?> //test2.php //该文件中有return语句 <?php $b = 'test2'; echo "被载入的文件:A 位置"; return $b; echo "<br 被载入的文件: B 位置"; ?>
结果:
相关推荐:
php 字符串写入文件或追加入文件(file_put_contents)
The above is the detailed content of What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code). For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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 originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool