PHP file contains

Posted by juuuugroid on Wed, 23 Feb 2022 02:21:14 +0100

File contains

Thinking: the function of PHP requires that the function must be called in memory, but the function is written in the corresponding PHP file one by one. Must the code in other files be copied to access it?

Introduction: if the corresponding function in a file has been written, if you want to use it in other PHP files, you must ensure that the code in the file enters memory and is related to each other. This uses php to provide a solution, that is, the file contains.

The document contains [Master]

Definition: file inclusion is to take the code from another PHP script in a PHP script to be run, and you can use the content in the included file, or use your own content in another included file.

1. Basic syntax of File Inclusion: PHP provides four methods of file inclusion, including and including_ Once, require and require_once, four of which are used in exactly the same way
include/include_once/require/equire_once 'file path and file name';
include/include_once/require/equire_once('File path and file name ');

<?php
//File contains
include 'index.php';    //Including index PHP file (current PHP file sibling directory)
?>

2. Meaning of document inclusion: there are two purposes of document inclusion

  • Include upward: that is, include a file first for the purpose of using the code or data in a file (using public code)
index.php
<?php
//Define function
function show(){
    echo 'hello world';
}
?>

include.php
<?php
//Including index PHP file
include_once 'index.php';
//Call index Functions in PHP
show();                    //Output hello world
?>
  • Downward inclusion: write the code first and then include the file. The purpose is to use the current data (using the generated data) in the included file
index.php
<?php
//get data
$info = 'hello world';
//Include file
include_once 'include.php';
?>

include.php
<html>
<head></head>
<body>
<?php
    echo $info;             //Output hello world
?>
</body>
</html>

3. Syntax difference of File Inclusion: all four inclusion methods can include files and use them

  • The difference between include and require is that if the included file does not exist, include will only alarm for errors without affecting the execution of its own code; And require will report fatal error and interrupt code execution
  • Include and include_once difference: include performs the include operation no matter what, and include_once will record whether the corresponding file has been included, and only operate on the same file multiple times (it is a good method for functions / classes that cannot be repeated).

4. Principle of File Inclusion: the essence of file inclusion is to introduce and run all the codes of the included file in the line of inclusion operation. However, the file inclusion statement is executed at runtime, so you cannot access the contents of the included file before including the file.

index.php
<?php
function show(){
    echo __FUNCTION__;
}
?>

include.php
<?php
show();                    //Error: the system cannot find the function
//introduce
include_once 'index.php';

show();                    //show: correct. Import before use
?>

5. Exercise: write the 99 multiplication table function in the a.php file, include the file in b.php, and then display the 99 multiplication table in the c file. That is, B contains both a and c

Summary: file inclusion is a real convenience for us to carry out modular development (code functions are separated into different files and managed and maintained by functions), and can realize the function of code reuse at the same time. It can help developers save a lot of time and workload.

Thinking: how to find the included file when the file is included? Should all development documents be put together?

Introduction: in actual development, it is possible to put all files together for very small projects, but it is impossible to put hundreds of files together for large and medium-sized projects. But also face the problem that the path between the user's local development and the actual deployment to the server is different. Therefore, you must pay attention to the path problem when the file is included.

Path problem [Master]

Definition: path problem refers to the problem of how PHP uses to find the file when the file is included. In the system, paths are usually divided into two types: absolute path and relative path.

1. Absolute path: there are two kinds of absolute paths: one is the local absolute path, and the other is the Internet absolute path

  • Local absolute path: the path from the drive letter root directory to the file. Under Windows is the logical drive letter, such as D: / server / Web / index php; Under Linux, it is the root directory, such as / home / Web / index php
  • Internet absolute path: URL, such as www.taobao.com com/index. php
D:/server/Web/index.php
<?php
function show(){
    echo __FUNCTION__;
}
?>

D:/server/Web/include.php
<?php
include_once 'D:/server/Web/index.php';    //Absolute path contains
?>

2. Relative path: the path of the included file relative to the current file. There are usually three types:

  • . /: indicates the same level directory (the folder to which the current file belongs), and there is "." under each folder File, representing the current directory
  • ... /: indicates the parent directory (the parent folder of the folder to which the current file belongs). There are "..." files under each folder, indicating the parent directory
  • Nothing: it means the same level directory. Distinction/ Lies in/ Will automatically match any directory Folder, and if there is nothing, it will only start from the directory where its own files are located (safer than. / Security)
D:/server/Web/index.php
<?php
function show(){
    echo __FUNCTION__;
}
?>

D:/server/Web/include.php
<?php
include_once 'index.php';                //Relative path contains
include_once './index.php';              //Relative path contains
?>

3. The difference between absolute path and relative path

  • High efficiency of relative path: the relative path can be found according to the offset of the current file position
  • The relative path is not safe: once the relative path is nested/ The current directory will change
  • Absolute path security: no path changes due to nesting
  • The absolute path efficiency is low: it must be searched layer by layer from the root directory

4. Nested inclusion of relative paths: relative paths/ And... / will change after the file is included (if it is included in different folders, it will not change under the same folder)

D:/server/Web/parent/son/son.php
<?php
echo __DIR__,'son';
?>

D:/server/Web/parent/parent.php
<?php
echo __DIR__,'parent';
include_once './son/son.php';        //Including son php
//At this point, you can access the parent PHP has no problem
?>

D:/server/Web/index.php
<?php
include_once './parent/parent.php';  
//At this point, visit index PHP error: parent PHP is indexed After PHP is included, its relative path changes/ Become index PHP path D:/server/Web, including/ son/son. The PHP path becomes D:/server/Web / son / son PHP, and this path does not exist

//Solution: 1 Not used. /; 2. Use absolute path
?>

5. Exercise: use absolute path and relative path for multi-level nested inclusion

Summary: not recommended for file inclusion/ And... / for file inclusion. Because it is inevitable to nest include files in actual development, and absolute path inclusion can perfectly avoid this problem. In addition, absolute path is recommended__ DIR__ Magic constant can ensure that code migration will not be limited by the operating system and disk.

Topics: PHP Back-end