[PHP]include detailed explanation_PHP tutorial
include loading file order
The order to look for included files is:
First search under include_path relative to the current working directory,
Then search under include_path relative to the directory where the currently running script is located.
For example, include_path is ., the current working directory is /includetest/, the script needs to include an includetest/a.php and there is an include "b.php" in the file, then the order of searching for b.php is first /includetest/, then is/includetest/include/. If the file name starts with ./ or ../, it will only be searched under the include_path relative to the current working directory.
The code structure is as follows:
~/public_html/includetest> ls -R
.:
a.php b.php include
./include:
a.php b.php where each file code is:
~/public_html/includetest> cat a.php
set_include_path(".");
include "include/a.php";
echo __FILE__ .PHP_EOL;
~/public_html/includetest> cat b.php
echo __FILE__ .PHP_EOL;
~/public_html/includetest> cat include/a.php
include "b.php";
echo __FILE__ .PHP_EOL;
~/public_html/includetest> cat include/b.php
echo __FILE__ .PHP_EOL;The output result is:
~/public_html/includetest> php a.php
# You can see include "b.php" in include/a.php; the includetest/b.php file will be loaded first
/home/zhanhailiang/public_html/includetest/b.php
/home/zhanhailiang/public_html/includetest/include/a.php
/home/zhanhailiang/public_html/includetest/a.php Note: The conceptual difference between the working directory and the directory where the script is run.
include variable scope
When a file is included, the code contained in it inherits the variable scope of the include line. From that point on, any variables available in the calling file at that line are also available in the called file. However, all functions and classes defined in include files have global scope.
The code is as follows:
~/public_html/includetest> cat c.php
include "d.php";
echo $d.PHP_EOL;
e();
~/public_html/includetest> cat d.php
$d = "hello world";
function e() {
echo "hello world, e()".PHP_EOL;
}
~/public_html/includetest> php c.php
hello world
hello world, e() Correspondingly, if include appears in a function in the calling file, all code contained in the called file will behave as if they were defined inside the function. So it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
include code execution
When a file is included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, and resumes at the end of the file. For this reason, any code in an object file that should be executed as PHP code must be included within valid PHP opening and closing tags.
include handles remote files
If "URL fopen wrappers" are enabled in PHP (the default configuration), it is possible to specify URLs (via HTTP or other supported wrapping protocols - see Supported Protocols and Wrapping Protocols for supported protocols) instead of local files. Contained files. If the target server interprets the target file as PHP code, you can pass variables to the included file using the URL request string for HTTP GET. This is not strictly the same thing as containing a file and inheriting the variable space of the parent file; the script file has actually been run on the remote server, and the local script includes its results.
include processing return value
Handling return values: You can use the return statement in an included file to terminate the execution of the program in the file and return to the script that called it. It is also possible to return values from included files. The return value of the include call can be obtained like a normal function. This does not work when including a remote file, however, unless the remote file's output has legal PHP start and end tags (like any local file). You can define the required variables within the tag, which will be available after the location where the file is included.
Because include is a special language construct, its parameters do not require parentheses. Be careful when comparing their return values.
If the file cannot be included, return FALSE and issue an E_WARNING warning.
If there are functions defined in include files, these functions can be used in the main file independently of whether they are used before or after return. If the file is included twice, PHP 5 issues a fatal error because the function is already defined, but PHP does not complain after return that the function is already defined. It is recommended to use include_once instead of checking if a file is included and returning it conditionally in the included file.
Another way to "include" a PHP file into a variable is to use an output control function in conjunction with include to capture its output.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
