search
HomeBackend DevelopmentPHP TutorialDetailed 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.

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 id="文件包含-被包含文件">文件包含——被包含文件</h3>
<?php header("Content-type:text/html;charset=gbk");
	$a = 2;$b = 4;
	define("xiaofeng",&#39;cool&#39;);

Contains file code

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

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

Included file code

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

Included file code

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


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
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!