php - file contains

content

First, the concept of file inclusion

2. The role of file inclusion

Three, the file contains four forms

(1) Include up - include the file first, then use the content in the file

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

Fourth, the 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)

2. Relative path: the path starting from the directory where the current file is located

3. The difference between absolute path and relative path loading

Five, file nesting contains

Six, the difference between Include and require

(1) The difference between Include and include_once:

(2) The difference between Require and include


First, the concept of file inclusion

        In a PHP script, to include another file (PHP), to cooperate to accomplish one thing.


2. The role of file inclusion

  • Either use the contents of the included file to achieve code sharing (reuse): include up (ask) include up: include other files before the current script uses a certain code
  • Either you have something you can use for other files to share (reuse) code: include downward (give) include downward: when you have something, you need another script to display it (after writing your own code) include other files)

The biggest role: division of labor and cooperation, each script does different things, so you can use the collaborative method to let multiple scripts work together to complete one thing.


Three, the file contains four forms

  • Include: include files
  • Include_once: The system will automatically determine whether the file has been included during the inclusion process (a file is included at most once)
  • Require: same as include
  • Require_once: same as include_once

(1) Include up - include the file first, then use the content in the file

Included file code

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

include file code

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

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

Included file code

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

include file code

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


Fourth, the principle of file loading

(1) Execution process of PHP code

  1. Read code file (PHP program)
  2. Compile: Convert PHP code to bytecode (generate opcode)
  3. zendengine parses opcode and performs logical operations according to bytecode
  4. Convert to the corresponding HTML code

(2) Principle of file loading

  • When the file is loaded (include or require), the system will automatically embed the code in the included file into the current file.
  • Loading location: where to load, the location where the code in the corresponding file is embedded is the corresponding include location
  • Included files in PHP are compiled separately

Note: If there is a syntax error during the compilation of the PHP file, it will fail (will not be executed); but if the included file has an error, the system will report an error when the include statement is included.

(3) File loading path

        When the file is loaded, the file path needs to be specified 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 files
  • Linux: /path/phpfile
  • Start from the website root directory (web absolute 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
  • ../: the parent directory (the previous folder of the current folder)

3. The difference between absolute path and relative path loading

1. The absolute path is relatively inefficient, but relatively safe (the path will not be a problem)

2. Relative paths are relatively efficient, but prone to errors (relative paths will change)


Five, file nesting contains

        A file contains another file, and the included file contains another file. When nested includes, it is easy to have a relative path error: the relative path will change due to the inclusion of the file (./ and ../): under windows, each folder has . and .. folders .


Six, the difference between Include and require

(1) The difference between Include and include_once:

  • The 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 multiple times, it will only be executed once.

(2) The difference between Require and include

The essence is to include files, the only difference is that when the files are not included, the form of the error is different.

  • Include has a lighter error level: does not prevent code execution
  • Require is more demanding: if the error code is included, it will not be executed (the code after require)

Guess you like

Origin blog.csdn.net/xiaofengdada/article/details/123787566