Home Backend Development PHP Tutorial [PHP]include detailed explanation_PHP tutorial

[PHP]include detailed explanation_PHP tutorial

Jul 14, 2016 am 10:09 AM
include php load Include Look for document yes of Table of contents Detailed explanation order

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477600.htmlTechArticleinclude loading file sequence The order in which to search for include files is: First, search under the include_path relative to the current working directory, and then It is the inclu relative to the directory where the currently running script is located...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles