Table of Contents
4. Principle of file loading
(1) Execution process of PHP code
(2) Principle of file loading
(3) File loading path
1. Absolute path: Start from the root directory of the disk (local absolute path) " >1. Absolute path: Start from the root directory of the disk (local absolute path)
3. The difference between loading absolute paths and relative paths" >3. The difference between loading absolute paths and relative paths
6. The difference between Include and require
(1) The difference between Include and include_once:" >(1) The difference between Include and include_once:
(2) The difference between Require and include" > (2) The difference between Require and include
Home Backend Development PHP Tutorial Detailed analysis of PHP file contains

Detailed analysis of PHP file contains

Apr 18, 2022 pm 05:57 PM
php

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.

Detailed analysis of PHP file contains

## Recommended study: "

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

  • 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
(1) Upward inclusion - include the file first, then use the content in the file

Code of the included file

<h3>文件包含——被包含文件</h3>
<?php
	header("Content-type:text/html;charset=gbk");
	$a = 2;$b = 4;
	define("xiaofeng",'cool');
Copy after login

Contains file code

<h3>文件包含——包含文件</h3>
<?php
	header("Content-type:text/html;charset=gbk");
	include "56.php";//包含文件56.php
	echo $a,"<hr>",$b,"<hr>",xiaofeng;
Copy after login

(2) Down Include - prepare content first, then include another file, and use the current content in the other file.

Included file code

<h3>文件包含——被包含文件</h3>
<?php
	header("Content-type:text/html;charset=gbk");
	echo $a,"<hr>",$b,"<hr>",xiaofeng;//输出数据
Copy after login

Included file code

<h3>文件包含——包含文件</h3>
<?php
	header("Content-type:text/html;charset=gbk");
	$a = 2;$b = 4;
	define("xiaofeng",&#39;cool&#39;);//定义数据
	include_once &#39;59.php&#39;;//包含数据为了显示以上的内容
Copy after login


4. Principle of file loading

(1) Execution process of PHP code

  1. Read Get the code file (PHP program)
  2. Compile: Convert PHP code into bytecode (generate opcode)
  3. zendengine parses the opcode and performs logical operations according to the bytecode
  4. 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!

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

Video Face Swap

Video Face Swap

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

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)

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles