Detailed analysis of PHP file contains
This article brings you relevant knowledge about PHP, which mainly introduces related issues about file inclusion, including the concept and function of file inclusion, and the four forms of file inclusion. As well as related content about the file loading principle, let’s take a look at it below. I hope it will be helpful to everyone.
PHP Video Tutorial"
1. The concept of file inclusion
In a PHP script, include another file (PHP) to complete one thing together.
2. The role of file inclusion
- Either use the contents of the included file , to realize code sharing (reuse): upward inclusion (request) upward inclusion: include other files before the current script uses a certain code
- #Or you have something you can give to others File usage to achieve code sharing (reuse): downward inclusion (given) downward inclusion: when you have something, you need other scripts to display it (including other files after your own code is written)
The biggest effect: division of labor and collaboration. Each script does different things, so you can use collaboration to let multiple scripts complete one thing together.
3. Four forms of file inclusion
(1) Upward inclusion - include the file first, then use the content in the file
- Include: Include the file
- Include_once: The system will automatically determine whether the file has been included during the inclusion process (a file can be included at most once)
- Require: Same as include
- ##Require_once: Same as include_once
Code of the included file<h3>文件包含——被包含文件</h3>
<?php
header("Content-type:text/html;charset=gbk");
$a = 2;$b = 4;
define("xiaofeng",'cool');
Contains file code<h3>文件包含——包含文件</h3>
<?php
header("Content-type:text/html;charset=gbk");
include "56.php";//包含文件56.php
echo $a,"<hr>",$b,"<hr>",xiaofeng;
Included file code<h3>文件包含——被包含文件</h3>
<?php
header("Content-type:text/html;charset=gbk");
echo $a,"<hr>",$b,"<hr>",xiaofeng;//输出数据
Included file code <h3>文件包含——包含文件</h3>
<?php
header("Content-type:text/html;charset=gbk");
$a = 2;$b = 4;
define("xiaofeng",'cool');//定义数据
include_once '59.php';//包含数据为了显示以上的内容
4. Principle of file loading
(1) Execution process of PHP code
- Read Get the code file (PHP program)
- Compile: Convert PHP code into bytecode (generate opcode)
- zendengine parses the opcode and performs logical operations according to the bytecode
- Convert into the corresponding HTML code
(2) Principle of file loading
- When the file is loaded (include or require), the system will automatically The code in the included file is equivalent to being embedded in the current file
- Loading location: Where to load, the location where the code in the corresponding file is embedded is The corresponding include location
- The files included in PHP are compiled separately
Note: If a syntax error occurs in the PHP file during the compilation process, it will fail (will not be executed); but if the included file has errors, the system will execute the include section. An error will be reported only when the statement is executed.
(3) File loading path
The file path needs to be specified when loading the file to ensure that PHP can correctly find the corresponding file.
1. Absolute path: Start from the root directory of the disk (local absolute path)
- Windows: drive letter C:/path/PHP file
- Linux:/path/PHP file
- Start from the website root directory (absolute network path)
- /: The path corresponding to the website host name
- Localhost/index.php -> E:/server/apache/htdocs/index.php
2 .Relative path: The path starting from the directory where the current file is located
- ./: Indicates the current folder
- . ./: Upper-level directory (the upper-level folder of the current folder)
3. The difference between loading absolute paths and relative paths
1. The absolute path is relatively inefficient, but relatively safe (the path will not cause problems)
##2 , Relative paths are relatively efficient, but prone to errors (relative paths will change)
5. Nested file inclusion
One file contains another file, and the included file contains another file. When nested includes, it is easy to have relative path errors: the relative path will change due to the inclusion of files (./ and ../): Under Windows, there are . and .. folders under each folder. .
6. The difference between Include and require
(1) The difference between Include and include_once:
- Include system will encounter it once and execute it once; if the same file is loaded multiple times, the system will execute it multiple times;
- Include_once: If the system encounters it multiple times, it will only be executed once.
(2) The difference between Require and include
The essence is both Include files, the only difference is that when the file cannot be included, the error form is different
- Include’s error level is relatively mild: it will not prevent code execution
- Require has higher requirements: if it contains error code, it will no longer be executed (the code after require)
Recommended study: "PHP video tutorial》
The above is the detailed content of Detailed analysis of PHP file contains. For more information, please follow other related articles on the PHP Chinese website!

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

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

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

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.
