Home  >  Article  >  Backend Development  >  Detailed analysis of PHP file contains

Detailed analysis of PHP file contains

WBOY
WBOYforward
2022-04-18 17:57:527341browse

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');

Contains file code

<h3>文件包含——包含文件</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>文件包含——被包含文件</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",&#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.net. If there is any infringement, please contact admin@php.cn delete