search
HomeBackend DevelopmentPHP Tutorial[PHP]include detailed explanation_PHP tutorial

[PHP]include detailed explanation_PHP tutorial

Jul 14, 2016 am 10:09 AM
includephploadIncludeLook fordocumentyesofTable of contentsDetailed explanationorder

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
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
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools