[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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
